Previous MAUI-Handling-Platform-specific-code MAUI-permissions-checklist Next

.NET MAUI Permissions

In .NET MAUI, permissions are the "gatekeepers" between your app and sensitive user data or hardware. Unlike older mobile systems where permissions were granted at install time, modern systems require Runtime Permissions.

1. The Two-Step Requirement

To access a protected feature (like the Camera or GPS), you must satisfy two conditions:

  • Static Declaration: You must list the permission in the platform manifest file (AndroidManifest.xml or Info.plist). If it’s not here, the app will crash immediately when requested.
  • Runtime Request: You must programmatically ask the user for "Allow" or "Deny" via a system popup.

2. The Permissions Class

MAUI provides the built-in Microsoft.Maui.ApplicationModel.Permissions class to handle permission logic. It uses a status-based flow:

  • CheckStatusAsync<T>: Checks if the permission is already granted without showing a popup.
  • ShouldShowRationale<T>: (Android only) Returns true if the user denied it once before; this tells you to explain why you need it before asking again.
  • RequestAsync<T>: Triggers the system dialog for user approval.

3. Basic Workflow Example

Here is the standard pattern for requesting Location permissions:

public async Task GetLocationPermission()
{
    // 1. Check current status
    PermissionStatus status =
        await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

    // 2. If not granted, request it
    if (status != PermissionStatus.Granted)
    {
        status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
    }

    // 3. Act on the result
    if (status == PermissionStatus.Granted)
    {
        // Access GPS
    }
    else
    {
        // Inform the user that the feature is disabled
    }
}
    

4. Common Permission Types

MAUI defines several built-in permission types you can pass as <T>:

  • Permissions.Camera
  • Permissions.StorageRead / Permissions.StorageWrite
  • Permissions.Photos
  • Permissions.Microphone
  • Permissions.Sensors

5. Critical Platform Differences

  • iOS / macOS: If a user selects "Deny", you cannot trigger the popup again. You must direct them to System Settings to enable it manually.
  • Android: You can request permissions multiple times, but eventually the OS provides a "Don't ask again" option.

Further Help

I can help you further if you'd like to:

  • See the exact XML tags needed for AndroidManifest.xml or Info.plist.
  • Learn how to create a Custom Permission for unsupported features.
  • Build a Permission Guard to prevent runtime crashes.

.NET MAUI Runtime Permissions

In .NET MAUI, permissions are a critical part of working with device APIs because many features (like location, camera, contacts, etc.) require explicit user consent at runtime. Let’s break down the basics of runtime permissions:

🔑 Why Permissions Matter

  • Mobile platforms (Android, iOS, macOS) enforce runtime permission checks for sensitive features.
  • Without permissions, APIs like Geolocation, MediaPicker, or Contacts will fail.
  • Windows apps often rely on system-level capabilities but may still require manifest declarations.

📌 How Permissions Work in MAUI

Declare in Manifest

  • Android → Add permissions in AndroidManifest.xml (e.g., ACCESS_FINE_LOCATION).
  • iOS/macOS → Add usage descriptions in Info.plist (e.g., NSLocationWhenInUseUsageDescription).
  • Windows → Add capabilities in Package.appxmanifest.

Request at Runtime

Use the Permissions API from MAUI Essentials.

await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
    

Check Before Use

Always check if permission is already granted:

var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
    status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}
    

🧩 Common Permissions

  • Location → Permissions.LocationWhenInUse, Permissions.LocationAlways
  • Camera → Permissions.Camera
  • Microphone → Permissions.Microphone
  • Storage / Photos → Permissions.StorageRead, Permissions.StorageWrite, Permissions.Photos
  • Contacts → Permissions.ContactsRead

⚠️ Best Practices

  • Request permissions only when needed (e.g., ask for location when the user opens a map).
  • Explain why — iOS requires a human-readable message in Info.plist.
  • Handle denial gracefully (provide fallback or disable the feature).
  • Test across platforms — Android and iOS handle permissions differently.

📍 Example: Location Permission

var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();

if (status != PermissionStatus.Granted)
{
    status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}

if (status == PermissionStatus.Granted)
{
    var location = await Geolocation.GetLocationAsync();
    Console.WriteLine($"Lat: {location.Latitude}, Lon: {location.Longitude}");
}
else
{
    Console.WriteLine("Location permission denied.");
}
    

👉 Permissions are the gateway to device APIs. Without them, your app cannot access hardware or sensitive data.

Back to Index
Previous MAUI-Handling-Platform-specific-code MAUI-permissions-checklist Next
*