Click here to Skip to main content
13,902,139 members
Click here to Skip to main content
Add your own
alternative version

Stats

2.8K views
8 downloads
3 bookmarked
Posted 11 Jan 2019
Licenced CPOL

Xamarin Forms: Check and Request for Permissions

, 11 Jan 2019
Rate this:
Please Sign up or sign in to vote.
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:

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);
         //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)

Share

About the Author

Harshit Gindra
Web Developer
United States United States
I'm an aspiring Developer recently started more and more interested towards ASP.NET and related technologies. I tend to love playing around with different technologies and get more hands on experience by working on different projects. My prime interest lies in Mobile Development(both native and hybrid) and have developed few basic application during my course of studies and work. Always up for a challenge and plan on to finish it through best possible approach.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01 | 2.8.190306.1 | Last Updated 11 Jan 2019
Article Copyright 2019 by Harshit Gindra
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid