Permissions Plugin for Xamarin
Simple cross platform plugin to request and check permissions.
Want to read about the creation, checkout my in-depth blog post.
Setup
- Available on NuGet: http://www.nuget.org/packages/Plugin.Permissions
- Install into your PCL/.NET Standard project and Client projects.
- Development NuGet: https://www.myget.org/feed/Packages/xamarin-plugins
Platform Support
Platform | Version |
---|---|
Xamarin.iOS | iOS 8+ |
Xamarin.Android | API 14+ |
Windows 10 UWP(Beta) | 10+ |
*See platform notes below
Android specific in your BaseActivity or MainActivity (for Xamarin.Forms) add this code:
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
Android Current Activity Setup
This plugin uses the Current Activity Plugin to get access to the current Android Activity. Be sure to complete the full setup if a MainApplication.cs file was not automatically added to your application. Please fully read through the Current Activity Plugin Documentation. At an absolute minimum you must set the following in your Activity's OnCreate method:
Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);
It is highly recommended that you use a custom Application that are outlined in the Current Activity Plugin Documentation](https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/master/README.md)
iOS Specific
Based on what permissions you are using, you must add information into your info.plist. Please read the Working with Security and Privacy guide for keys you will need to add.
Due to API usage it is required to add the Calendar permission :(
<key>NSCalendarsUsageDescription</key>
<string>Needs Calendar Permission</string>
You may also see this for: NSBluetoothPeripheralUsageDescription
which you may also have to add.
Even though your app may not use calendar at all. I am looking into a workaround for this in the future.
API Usage
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);
In Action
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(Permission.Location);
//Best practice to always check that the key exists
if(results.ContainsKey(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;
}
Available Permissions
/// <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
IMPORTANT
Android:
You still need to request the permissions in your AndroidManifest.xml. Also ensure your MainApplication.cs was setup correctly from the CurrentActivity Plugin.
Windows 10 UWP
UWP has a limited set of supported permissions. You can see the documentation above, but current support: Contacts, Location, and Sensors.
Contributors
- Icon thanks to Jérémie Laval
Thanks!
License
Licensed under main repo license(MIT)
Want To Support This Project?
All I have ever asked is to be active by submitting bugs, features, and sending those pull requests down! Want to go further? Make sure to subscribe to my weekly development podcast Merge Conflict, where I talk all about awesome Xamarin goodies and you can optionally support the show by becoming a supporter on Patreon.