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/.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
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);
}
This plugin uses the Xamarin.Essentials, please follow the setup guide.
Xamarin.Essentials.Platform.Init(this, bundle);
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.
You are able to check and requests permissions with just a few lines of code:
Check permission:
PermissionStatus status = await CrossPermissions.Current.CheckPermissionStatusAsync<CalendarPermission>();
Request permission:
PermissionStatus status = await CrossPermissions.Current.RequestPermissionAsync<CalendarPermission>();
Additionally on Android there is a situation where you may want to detect if the user has already declined the permission and you should show your own pop up:
bool shouldShow = await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Calendar);
- CalendarPermission
- CameraPermission
- ContactsPermission
- LocationPermission
- LocationAlwaysPermission
- LocationWhenInUsePermission
- MediaLibraryPermission
- MicrophonePermission
- PhonePermission
- PhotosPermission
- RemindersPermission
- SensorsPermission
- SmsPermission
- StoragePermission
- SpeechPermission
Here is how you may use it with geolocation:
try
{
var status = await CrossPermissions.Current.CheckPermissionStatusAsync<LocationPermission>();
if (status != PermissionStatus.Granted)
{
if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Location))
{
await DisplayAlert("Need location", "Gunna need that location", "OK");
}
status = await CrossPermissions.Current.RequestPermissionAsync<LocationPermission>();
}
if (status == PermissionStatus.Granted)
{
//Query permission
}
else if (status != PermissionStatus.Unknown)
{
//location denied
}
}
catch (Exception ex)
{
//Something went wrong
}
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.
- Icon thanks to Jérémie Laval
Thanks!
Licensed under main repo license(MIT)
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.