React Native Runtime PermissionsAndroid Request Android Example Tutorial

Android application has its own secure infrastructure to make application user feel that he is completely secure from any threat and also he can manage to give some of his own data as his own permissions. Here comes the new android’s Dangerous runtime permissions request group in which the application developer directly ask the application user for his permission before accessing his personal data like Device Location, Access Camera, Access device storage, Access device Contacts etc. In this tutorial we would going to make a React Native Runtime PermissionsAndroid Request Android Example Tutorial. We would use react native’s own PermissionsAndroid component to implement all the runtime permissions in android. So let’s get started .

Note: These permissions would only work in device which has android operating system grater than Marshmallow or has Marshmallow 6.0 API Level 23. Below API Level 23 it will automatically grant all the permissions. Here is the list of all available permissions in Android.

Contents in this project React Native Runtime PermissionsAndroid Request Android Example Tutorial:

1. The first step is to add the permission itself in AndroidManifest.xml file, So open YourReactNativeProject-> android -> app -> src -> main -> AndroidManifest.xml file. We are creating this example with device location permission so we are adding the device location permission known as ACCESS_FINE_LOCATION in our AndroidManifest.xml file. You have to add your own permission here.

2. Now we need to specify android:minSdkVersion and android:targetSdkVersion in our AndroidManifest.xml . This step is very important.

Code of AndroidManifest.xml file after adding above all codes:

3. Open YourReactNativeProject -> android -> app -> build.gradle file and find targetSdkVersion and set its to 23 like targetSdkVersion 23 in default Config block.

4. Open your project’s App.js file and import Platform, StyleSheet, View, PermissionsAndroid, Text and Alert component in your project.

5. Create a Async function name as request_location_runtime_permission() with export syntax out of your main class. This function has its own body and defined individually out the main class body, This function is used to request runtime permission.

6. Create the try and catch block to handle the exception generated runtime and then we need to request the runtime permission. As you can see in below code we have make a constant named as granted which would store the result after enabling and disabling the result.

PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION : Used to access the runtime permission, You need to define your own permission here and most important thing is the same permission you need to add in AndroidManifest.xml file.

7. Create main export View class named as App . This is our main class and we would call the above permissions functions inside this class. As you can see in below class code we are calling the request_location_runtime_permission() inside the componentDidMount() function. we need to make the componentDidMount() async to use the async function and to call the async function we have to use await keyword.

8. Code for Style CSS.

9. Complete source code for App.js File :

Screenshots:

React Native Runtime PermissionsAndroid Request Android Example Tutorial