Skip to main content

Ionic Requesting Permissions At Run Time

Share on FacebookTweet about this on TwitterShare on Google+Share on RedditShare on StumbleUponDigg thisFlattr the authorPin on PinterestShare on TumblrShare on LinkedInPrint this pageBuffer this page

In this post we will take a look how you can request for Permissions at run time for Android. I will point you to the correct section of the docs for iOS. If the device on which you app is intalled is running Android 5.1 (API 22) or lower then the user will need to grant permissions when the app is being installed from the PlayStore. If the device is running Android 6.0 (API 23) or higher then the app must request each permission while running. This has been implemented by android to streamline the installation process.

There are two plugin that are used for managing permission:

1) Android Permissions

This plugin is an Android-only plugin. I was unable to make this plugin work. If you want to give it a try you can check the docs here.

2) Diagnostic

This plugin can be used for Android, iOS and Windows 10 Mobile. This is the plugin that I recommend and we will be using this in this post. Check out the plugin docs here.

 

We will create a an app that will request for the camera permission. As mentioned here I will create an example only for android you can check the docs for iOS and Windows Mobile.

 

Step 1)

We will create a new project my running the following command.

 

Step 2)

Next we will add the diagnostic plugin and install the ionic native wrapper with the following command.

Step 3)

We will need to add a platform to our app. Here I will be just adding Android, you can add iOS and Windows as well.

 

Step 4)

For Android, we will need to add the permission that we want to request to the AndroidManifest.xml. You can either directly add permission to the AndroidManifest.xml file or make use of the config.xml file to do so, I recommend taking the second approch. I have a detailed post on Adding Tags To Android Manifest File For Ionic which you can read and add the android.permission.CAMERA  permission.

 

Step 5)

First of all we will need to add the plugin to app.module.ts file we do that as follows.

 

Step 6)

Now it is time to code up our app. We will create a button, which when pressed request for the camera permission. So open up app/pages/home/home.html and code it as follows.

 

Step 7)

Now let’s code up the function that will request the permission. Open up app/pages/home/home.ts and code it as follows.

Here are we are first checking if the permission has been granted or not using  diagnostic.getPermissionAuthorizationStatus(). If it is not granted then we are requsting the permission using diagnostic.requestRuntimePermission(). This will give output of the constants “GRANTED” or “DENIED”. There are other constats as well, you can take alook at the full list here.

When we finally have the permission we are just logging out “We have the permission”, this is where you can use the camera API.

 

What To Do When Permission Is Denied

When the user denies permission to a certain native feature. It is best to display the user with a dialog box explaining why the app needs the permission. Also, make sure to inform the user that the app might not function correctly if the permission is Denied.

 

Things To Keep In Mind

Keep in mind that at any point the user can revoke the permissions granted. So, it is best practice to check for permission every time before using a native feature even if the user has granted permission once.

Here we have requested the Camera permission by passing this.diagnostic.permission.CAMERA into the getPermissionAuthorizationStatus() method. You can get the full list of constants here.  Alternatively you can use the isCameraAuthorized() and requestCameraAuthorization() methods offerd by the pluging to check and request for the camera permission as well.

You can request for more that one permission at a time using the diagnostic.requestRuntimePermissions() method.

The diagnostic plugin also has a method diagnostic.registerPermissionRequestCompleteHandler() this method will fire every time a permissions are requestied for. It will return the current status of all the requested permission. This can be usefull in keeping track of all the permissions at one place.

 

Platform Specefic Permissions

There are a bunch of common permissions that you can request for in both Android and iOS .You can take a look at list of those in the docs here. For iOS only permission see here. For Android only permssions see here.

Conclusion

Requesting for runtime permssions is not complicated at all. Make sure you have the permission that you request for added in the AndroidManifest file. The cordova-diagnostic-plugin not only offers a way to check for permissons but it also has a large number of methods that you use to check for permissions that may make your life easier.

 

Download Code:


Code Will Be Sent Via Email

 

Leave Me Your Questions And Feedback

Getting your feedback inspires me the most so hit me up in the comments here or on the comments section on my YouTube Channel or on twitter @pointDeveloper

 

Consider Subscribing / Donating
Subscribe To Mailing List
Donate
If my site was helpful to you, then please consider donating via paypal:

Prantik Vaghela (pointdeveloper)

Hi my name is Prantik Vaghela. I have done my Bachelors and Masters in Computer Science and am a web developer and now a Tutor through my blog. :-)

%d bloggers like this: