| MAUI-Handling-Platform-specific-code | MAUI-permissions-checklist | |
.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.
To access a protected feature (like the Camera or GPS), you must satisfy two conditions:
MAUI provides the built-in Microsoft.Maui.ApplicationModel.Permissions class to handle permission logic. It uses a status-based flow:
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
}
}
MAUI defines several built-in permission types you can pass as <T>:
I can help you further if you'd like to:
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:
Use the Permissions API from MAUI Essentials.
await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
Always check if permission is already granted:
var status = await Permissions.CheckStatusAsync<Permissions.LocationWhenInUse>();
if (status != PermissionStatus.Granted)
{
status = await Permissions.RequestAsync<Permissions.LocationWhenInUse>();
}
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.
| MAUI-Handling-Platform-specific-code | MAUI-permissions-checklist | |