Simple cross platform plugin to request and check permissions.
Want to read about the creation, checkout my in-depth blog post.
- Available on NuGet: http://www.nuget.org/packages/Plugin.Permissions
- Install into your PCL project and Client projects.
- Development NuGet: https://ci.appveyor.com/nuget/permissionsplugin
Platform Support
Platform | Supported | Version |
---|---|---|
Xamarin.iOS | Yes | iOS 7+ |
Xamarin.Android | Yes | API 14+ |
Windows Phone Silverlight | --- | 8.0+ |
Windows Phone RT | --- | 8.1+ |
Windows Store RT | --- | 8.1+ |
Windows 10 UWP | Yes* | 10+ |
Xamarin.Mac | No |
*See platform notes below
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
You MUST set your Target version to API 23+ and Compile against API 23+:
When building against the iOS 10 SDK (Xcode 8) please be aware of the platform privacy changes. Based on what permissions you are using, you must add information into your info.plist. Please read the following blog for more information.
Due to API usage it is required to add the Calendar permission :(
NSCalendarsUsageDescription Needs Calendar Permission
Even though your app may not use calendar at all. I am looking into a workaround for this in the future.
Call CrossPermissions.Current from any project or PCL to gain access to APIs.
Should show request rationale
/// <summary>
/// Request to see if you should show a rationale for requesting permission
/// Only on Android
/// </summary>
/// <returns>True or false to show rationale</returns>
/// <param name="permission">Permission to check.</param>
Task<bool> ShouldShowRequestPermissionRationaleAsync(Permission permission);
CheckPermissionStatus
/// <summary>
/// Determines whether this instance has permission the specified permission.
/// </summary>
/// <returns><c>true</c> if this instance has permission the specified permission; otherwise, <c>false</c>.</returns>
/// <param name="permission">Permission to check.</param>
Task<PermissionStatus> CheckPermissionStatusAsync(Permission permission);
RequestPermissions
/// <summary>
/// Requests the permissions from the users
/// </summary>
/// <returns>The permissions and their status.</returns>
/// <param name="permissions">Permissions to request.</param>
Task<Dictionary<Permission, PermissionStatus>> RequestPermissionsAsync(params Permission[] permissions);
Here is how you may use it with the Geolocator Plugin:
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Location);
if (status != PermissionStatus.Granted)
{
if(await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
var results = await CrossPermissions.Current.RequestPermissionsAsync(new[] {Permission.Location});
status = results[Permission.Location];
}
if (status == PermissionStatus.Granted)
{
var results = await CrossGeolocator.Current.GetPositionAsync(10000);
LabelGeolocation.Text = "Lat: " + results.Latitude + " Long: " + results.Longitude;
}
else if(status != PermissionStatus.Unknown)
{
await DisplayAlert("Location Denied", "Can not continue, try again.", "OK");
}
}
catch (Exception ex)
{
LabelGeolocation.Text = "Error: " + ex;
}
/// <summary>
/// Permission group that can be requested
/// </summary>
public enum Permission
{
/// <summary>
/// The unknown permission only used for return type, never requested
/// </summary>
Unknown,
/// <summary>
/// Android: Calendar
/// iOS: Calendar (Events)
/// UWP: None
/// </summary>
Calendar,
/// <summary>
/// Android: Camera
/// iOS: Photos (Camera Roll and Camera)
/// UWP: None
/// </summary>
Camera,
/// <summary>
/// Android: Contacts
/// iOS: AddressBook
/// UWP: ContactManager
/// </summary>
Contacts,
/// <summary>
/// Android: Fine and Coarse Location
/// iOS: CoreLocation (Always and WhenInUse)
/// UWP: Geolocator
/// </summary>
Location,
/// <summary>
/// Android: Microphone
/// iOS: Microphone
/// UWP: None
/// </summary>
Microphone,
/// <summary>
/// Android: Phone
/// iOS: Nothing
/// UWP: None
/// </summary>
Phone,
/// <summary>
/// Android: Nothing
/// iOS: Photos
/// UWP: None
/// </summary>
Photos,
/// <summary>
/// Android: Nothing
/// iOS: Reminders
/// UWP: None
/// </summary>
Reminders,
/// <summary>
/// Android: Body Sensors
/// iOS: CoreMotion
/// UWP: Device Access Sensor Class
/// </summary>
Sensors,
/// <summary>
/// Android: Sms
/// iOS: Nothing
/// UWP: None
/// </summary>
Sms,
/// <summary>
/// Android: External Storage
/// iOS: Nothing
/// UWP: None
/// </summary>
Storage
/// <summary>
/// Android: Microphone
/// iOS: Speech
/// UWP: None
/// </summary>
Speech
}
Read more about android permissions: http://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
You still need to request the permissions in your AndroidManifest.xml. Also ensure your MainApplication.cs was setup correctly from the CurrentActivity Plugin.
UWP has a limited set of supported permissions. You can see the documentation above, but current support: Contacts, Location, and Sensors.
These contain blank implementation and just return Granted status.
- Icon thanks to Jérémie Laval
Thanks!
Licensed under main repo license(MIT)