NativeScript Request Permissions For Android

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 tutorial we will discuss how you can request for permissions for your app to access certain device features on Android. There are two ways we will be requesting for permissons, the first is using the AndroidManifest.xml file and the second is using a plugin.

It is important here to mention that for Android API 22 and below you can get permissions by adding them to the AndroidManifest.xml file only. But for Android API 23 and above you will need to request the user for permission not only by adding it to the AndroidManifest.xml file but also the permission has to be requested at runtime.

Let’s code a sample app and take a look how this works.

Step 1)

Open you command prompt/Terminal and run the following command.

Here I am creating a new project named requestPermisson with the --ng option, that will create the project with angular 2. We are also navigating inside the project folder.

 

Step 2)

Now let’s add the Android platform to our project by issuing the following command.

Step 3)

For the sake of this example let’s say that we need to access the network state and camera. You can get the full list of permissions in the Android docs here. To get access to camera and network state we will need to request it in the AndroidManifest.xml file. So open up app/App_Resources/android/AndroidManifest.xml. It will look as follows

 

As you can see there are already some permissions added to it. So for our camera and network state we will add the following lines of code.

Note here that the permission name has to be in uppercase. Also, make sure that you do not remove any existing <uses-permission> tag. You can take a look at all the permissions that you can request in the docs.

 

If you wish to support only apps that are using Android API 22 or below, then you are done, no need to do anything else. But If you wish to support Android API 23 and above you will need to request permission at runtime as I mentioned at the starting of the post.

 

Step 4)

For Android API 23 and up we will need to add the nativescript-permissions plugin. To add the plugin open up prompt/Terminal and run the following command inside your project folder.

The plugin offers three functions

  • hasPermisson()  – This function checks to see if the permission has been granted or not.
  • requestPermission() – This function is used to request a single permission.
  • requestPermissions() – This function is used to request a multiple permissions.

I encourage you to read the docs for more info.

 

Step 5)

Now let’s use the plugin to request for permissions at runtime. To do this in the sample app open up app.component.html and code it as follows

Here we have added a button to the default app. This button when tapped, will call the getPermission() function which we will code in the next step.

 

Step 6)

Now it’s time code the app.component.ts file where we will use the plugin to request for permissions at runtime. We will have the following code in app.component.ts

Let’s understand what we are doing in the above code. Firstly, we are importing the plugin using the import statement.

Then we are declaring android as variable. Doing so will get rid of typescript errors.

Then at the bottom, we are coding the getPermission() function. Notice that we are using the permissions.requestPermissions() function which will request multiple permissions. As we are using that function the permissions are passed in as an array. We have also passed a string that will appear when the app is requesting permission, you can use this to notify the user why the app needs the requested permission.

As I have mentioned above you will need to request for the same permission in the AndroidManifest.xml as well at runtime. For instance, you cannot request to access the Location at runtime when you have requested to access the camera in the manifest file. Both have to be the same.

And that’s it. You have successfully requested the user for permissions. Now I know that this is not a real world example, but its’s the best way to explain how you can make things work.

 

Step 7)

Now its time to run our app, we can do that by using the run or the livesync command as follows

Before running the commands make sure that you have your emulator running in the background or you have a device connected with debugging enabled.

Conclusion

Whenever accessing devices features of apps it is necessary that the app request it from the user before using that feature. Otherwise, your app won’t be able to use it. Moreover keep in mind that the user can revoke permissions granted anytime, so it is better to check whether your app has the permission to use a particular feature or not every time you code to use it using the hasPermission() function.

 

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: