Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Xamarin

Xamarin Forms: Check and Request for Permissions

5.00/5 (1 vote)
11 Jan 2019CPOL1 min read 41.3K   11  
Check and request for Permissions in Xamarin Forms mobile application

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 Permissions for Calendar, here's something you can use:

C#
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.

C#
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.

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:

C#
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:

C#
[Application]
 public class MainApplication : Application, Application.IActivityLifecycleCallbacks
 {
     public MainApplication(IntPtr handle, JniHandleOwnership transer)
       : base(handle, transer)
     {
     }

     public override void OnCreate()
     {
         base.OnCreate();
         RegisterActivityLifecycleCallbacks(this);
         //A great place to initialize Xamarin.Insights and Dependency Services!
     }

     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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)