Introduction
This article provides a solution to check and request for Permissions from Xamarin.Forms
mobile application.You can check out this video tutorial to see the code in action.
Background
Before proceeding with this article, please go through some basics of Xamarin.Forms
, Xamarin.Android
and Xamarin.iOS
. We'll be using a plugin called Plugins.Permission for most of the functionality.
Using the Code
Here, we will be using two clients (Android and iOS).
Xamarin.Portable
Checking Permission Status
CheckPermissionStatusAsync
method will provide the status of the Permission
on the device. For example, to check Permission
s for Calendar
, here's something you can use:
var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Calendar);
Requesting Permissions
RequestPermissionsAsync
method can be used to request for any particular permissions. You can also request multiple permissions at the same time.
var response = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Calendar);
If the User
has denied the permission, you might not be able to request for permissions again. The behavior is different on Android and iOS.
Android Implementation
To get started, first we need to make sure that the permissions our application will require are added in AndroidManifest.XML.
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
We need to override OnRequestPermissionsResult
method in MainActivity.cs with 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);
}
Now, we need to create MainApplication.cs
class and with the following code:
[Application]
public class MainApplication : Application, Application.IActivityLifecycleCallbacks
{
public MainApplication(IntPtr handle, JniHandleOwnership transer)
: base(handle, transer)
{
}
public override void OnCreate()
{
base.OnCreate();
RegisterActivityLifecycleCallbacks(this);
}
public override void OnTerminate()
{
base.OnTerminate();
UnregisterActivityLifecycleCallbacks(this);
}
public void OnActivityCreated(Activity activity, Bundle savedInstanceState)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivityDestroyed(Activity activity)
{
}
public void OnActivityPaused(Activity activity)
{
}
public void OnActivityResumed(Activity activity)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivitySaveInstanceState(Activity activity, Bundle outState)
{
}
public void OnActivityStarted(Activity activity)
{
CrossCurrentActivity.Current.Activity = activity;
}
public void OnActivityStopped(Activity activity)
{
}
}
iOS Implementation
Just like AndroidManifest.xml, we need to specify the permissions in Info.plist
. If not specified, our application won't be able to access the state of Permission
and will crash.
Privacy - Calendars Usage Description
Privacy - Camera Usage Description
After specifying all required permissions, we're all set to set our iOS application.