# PermissionsPlugin **Repository Path**: alan69alan69/PermissionsPlugin ## Basic Information - **Project Name**: PermissionsPlugin - **Description**: Check and Request Permissions Plugin for Xamarin and Windows - **Primary Language**: C# - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-03-10 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## 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](http://motzcod.es/post/133939517717/simplified-ios-android-runtime-permissions-with). ### Setup * Available on NuGet: http://www.nuget.org/packages/Plugin.Permissions [![NuGet](https://img.shields.io/nuget/v/Plugin.Permissions.svg?label=NuGet)](https://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 Build Status: ![Build status](https://jamesmontemagno.visualstudio.com/_apis/public/build/definitions/6b79a378-ddd6-4e31-98ac-a12fcd68644c/19/badge) ### Android specific in your BaseActivity or MainActivity (for Xamarin.Forms) add this code: ```csharp 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](https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/master/README.md) 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](https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/master/README.md). At an absolute minimum you must set the following in your Activity's OnCreate method: ```csharp 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](https://developer.xamarin.com/guides/ios/application_fundamentals/security-privacy-enhancements/). Due to API usage it is required to add the Calendar permission :( ``` NSCalendarsUsageDescription Needs Calendar Permission ``` 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** ```csharp /// /// Request to see if you should show a rationale for requesting permission /// Only on Android /// /// True or false to show rationale /// Permission to check. Task ShouldShowRequestPermissionRationaleAsync(Permission permission); ``` **CheckPermissionStatus** ```csharp /// /// Determines whether this instance has permission the specified permission. /// /// true if this instance has permission the specified permission; otherwise, false. /// Permission to check. Task CheckPermissionStatusAsync(Permission permission); ``` **RequestPermissions** ```csharp /// /// Requests the permissions from the users /// /// The permissions and their status. /// Permissions to request. Task> RequestPermissionsAsync(params Permission[] permissions); ``` ### In Action Here is how you may use it with the Geolocator Plugin: ```csharp 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 ```csharp /// /// Permission group that can be requested /// public enum Permission { /// /// The unknown permission only used for return type, never requested /// Unknown, /// /// Android: Calendar /// iOS: Calendar (Events) /// UWP: None /// Calendar, /// /// Android: Camera /// iOS: Photos (Camera Roll and Camera) /// UWP: None /// Camera, /// /// Android: Contacts /// iOS: AddressBook /// UWP: ContactManager /// Contacts, /// /// Android: Fine and Coarse Location /// iOS: CoreLocation (Always and WhenInUse) /// UWP: Geolocator /// Location, /// /// Android: Microphone /// iOS: Microphone /// UWP: None /// Microphone, /// /// Android: Phone /// iOS: Nothing /// UWP: None /// Phone, /// /// Android: Nothing /// iOS: Photos /// UWP: None /// Photos, /// /// Android: Nothing /// iOS: Reminders /// UWP: None /// Reminders, /// /// Android: Body Sensors /// iOS: CoreMotion /// UWP: Device Access Sensor Class /// Sensors, /// /// Android: Sms /// iOS: Nothing /// UWP: None /// Sms, /// /// Android: External Storage /// iOS: Nothing /// UWP: None /// Storage /// /// Android: Microphone /// iOS: Speech /// UWP: None /// 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](https://github.com/garuma) 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](http://mergeconflict.fm), where I talk all about awesome Xamarin goodies and you can optionally support the show by becoming a [supporter on Patreon](https://www.patreon.com/mergeconflictfm).