Hello Readers, CoolMonkTechie heartily welcomes you in this article ( How To Apply Run Time Android Permission In React Native ).
In this article, we will learn how to apply Run Time Android Permission In React Native. Android Permission feature helps a user to know which application is trying to access sensitive data such as files, contacts and SMS, and system-specific features such as camera and internet. This article shows to apply the run time permission with the authentic example.
To understand the Run Time Android Permission In React Native, we cover the below topics as below :
- What is Android Permission?
- Why we need permission?
- How to apply Permission in Android?
- How to apply run time Permission in React Native?
- Example to apply the run time permission.
A famous quote about learning is :
” The great aim of education is not knowledge, but action. “
So Let’s begin.
What is Android Permission ?
According to Google’s privacy policy, a user should know which application is trying to access sensitive data such as files, contacts and SMS, and system-specific features such as camera and internet. So they introduced the Android Permission feature.
In Android permission modal, every application which is using sensitive data or some certain system features has to ask the permissions from the user.
The purpose of permission is to protect the privacy of an Android user.
Why We need Permission ?
Android applies the permission modal because some applications were tracking the user’s location in the background and accessing the private data like contacts, call history and messages without informing the user which is the violation of googles privacy policy.
So solving this sensitive issue application has to ask for permission to access those sensitive data.
How to Apply Permission in Android ?
Now we get an idea about the Android permission, let’s see how to apply the permission in Android.
To apply permission in Android, we have to follow two steps:
- We have to add those permissions requests in
project -> app -> src -> AndroidManifest.xml
file.
For Example, if we want to ask for the device location Permission, we have to add the following permission request in AndroidManifest.xml :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
After adding the permission in AndroidManifest file, this permission will be automatically asked by the play store when they install the app and this permission will be granted to the applications.
On devices before SDK version 23, the permissions are automatically granted if we add those permissions in our AndroidManifest.xml file, but after SDK version 23 we have to ask runtime permission as well.
2. After Google’s new permission modal, they have introduced the run time permission mechanism in Android version 23. According to that, we have to include all the permissions in AndroidManifest.xml and also have to apply some dangerous permissions in run time.
According to the official docs, dangerous permissions require a dialog prompt.
So whenever we are working with this dangerous permissions, we need to check whether the permission is granted by the user, and that can be done with the help of PermissionsAndroid
in React Native.
For example, If we need INTERNET permission and CAMERA permission then we have to add both the permission in AndroidManifest.xml file but have to ask only for the CAMERA permission in run time because CAMERA permission comes under the dangerous permission not INTERNET permission.
Run Time Permissions that requires Confirmation from the User. Here is the list of dangerous permissions.
READ_CALENDAR | android.permission.READ_CALENDAR |
WRITE_CALENDAR | android.permission.WRITE_CALENDAR |
CAMERA | android.permission.CAMERA |
READ_CONTACTS | android.permission.READ_CONTACTS |
WRITE_CONTACTS | android.permission.WRITE_CONTACTS |
GET_ACCOUNTS | android.permission.GET_ACCOUNTS |
ACCESS_FINE_LOCATION | android.permission.ACCESS_FINE_LOCATION |
ACCESS_COARSE_LOCATION | android.permission.ACCESS_COARSE_LOCATION |
RECORD_AUDIO | android.permission.RECORD_AUDIO |
READ_PHONE_STATE | android.permission.READ_PHONE_STATE |
CALL_PHONE | android.permission.CALL_PHONE |
READ_CALL_LOG | android.permission.READ_CALL_LOG |
WRITE_CALL_LOG | android.permission.WRITE_CALL_LOG |
ADD_VOICEMAIL | com.android.voicemail |
USE_SIP | android.permission.USE_SIP |
PROCESS_OUTGOING_CALLS | android.permission.PROCESS_OUTGOING_CALLS |
BODY_SENSORS | android.permission.BODY_SENSORS |
SEND_SMS | android.permission.SEND_SMS |
RECEIVE_SMS | android.permission.RECEIVE_SMS |
READ_SMS | android.permission.READ_SMS |
RECEIVE_WAP_PUSH | android.permission.RECEIVE_WAP_PUSH |
RECEIVE_MMS | android.permission.RECEIVE_MMS |
READ_EXTERNAL_STORAGE | android.permission.READ_EXTERNAL_STORAGE |
WRITE_EXTERNAL_STORAGE | android.permission.WRITE_EXTERNAL_STORAGE |
How to apply Run Time Android Permission using React Native PermissionsAndroid ?
In React Native, PermissionsAndroid
component provides access to Android M’s (Over API level 23) new permissions model. We always need to check the permission before using native APIs which comes under dangerous permissions.
Above mentioned all the dangerous permissions comes under PermissionsAndroid.PERMISSIONS
as constants.
We can check the permission granted or not using PermissionsAndroid.RESULTS.GRANTED
.
To apply the permission to use
PermissionsAndroid.request(permission name,{Permission dialog heading, body})
Result Strings for Requesting Permissions
As constants under PermissionsAndroid.RESULTS
:
Response | Result |
---|---|
GRANTED | Permission granted successfully by the user |
DENIED | Permission denied by the user |
NEVER_ASK_AGAIN | Permission denied by the user with never ask again. |
Now we get an idea about Android permissions and components. Let’s move towards the Example which can help us apply permission in our application.
Example to Apply the Run Time Permission
In this example, we will apply the device location permission, which needs run time permission. We are making a button on the centre of the screen and on a click of button we will apply the run time permission for device location known as ACCESS_FINE_LOCATION. So let’s get started.
Example Project Setup
To demonstration of apply the Run Time Permission, we have to follow the below steps:
- Create a new React Native project
- Adding device location permission in AndroidManifest.xml
1. Create a new React Native project
Assuming that we have node installed, we can use npm to install the react-native-cli
command line utility. Open the terminal and go to the workspace and run the following commands to create a new React Native project.
npx react-native init ProjectName
This will make a project structure with an index file named App.js in our project directory.
2. Adding device location permission in AndroidManifest.xml
If we are using any features in our app that needs permission, we need to add it in AndroidManifest.xml
. For this example, we are adding device location permission in AndroidManifest.xml.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Now jump into the project using
cd ProjectName
Example Code to Apply for the Run Time Permission
Open App.js in any code editor and replace the code with the following code.
App.js
import React, {Component} from 'react';
import {
Platform,
StyleSheet,
View,
PermissionsAndroid,
Text,
Alert,
} from 'react-native';
export async function request_location_runtime_permission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'ReactNativeCode Location Permission',
message: 'ReactNativeCode App needs access to your location ',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
Alert.alert('Location Permission Granted.');
} else {
Alert.alert('Location Permission Not Granted');
}
} catch (err) {
console.warn(err);
}
}
export default class App extends Component {
async componentDidMount() {
await request_location_runtime_permission();
}
render() {
return (
<View style={styles.MainContainer}>
<Text>React Native Runtime Permission Request</Text>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
flex: 1,
paddingTop: Platform.OS === 'ios' ? 20 : 0,
justifyContent: 'center',
margin: 20,
},
});
Here PermissionsAndroid.request
has two arguments:
- PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION to generate a dialog to ask for Device Location Permission.
- A JSON {‘title’:”,’message’:”}, which will help us to communicate or to show the dialog about the permission if the user denied the permission. They will generate it like this.
To Run the React Native Example Code
Open the terminal again, and jump into our project using
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
npx react-native run-android
or on the iOS Simulator by running (macOS only)
npx react-native run-ios (macOS only).
The output of example code is as below:
That’s all about in this article.
Conclusion
In this article, we understood how to apply Run Time Android Permission in React Native. This article showed the example code to apply the RunTime Android Permission in React Native.
Thanks for reading! I hope you enjoyed and learned about RunTime Android Permission Concepts in React Native. Reading is one thing, but the only way to master it is to do it yourself.
Please follow and subscribe us on this blog and support us in any way possible. Also like and share the article with others for spread valuable knowledge.
You can find Other articles of CoolmonkTechie as below link :
You can also follow official website and tutorials of React Native as below links :
If you have any comments, questions, or think I missed something, feel free to leave them below in the comment box.
Thanks again Reading. HAPPY READING!!???
Hello there! This post couldn’t be written any better! Reading through this article reminds me of my previous roommate!
He continually kept preaching about this. I am going to forward this post
to him. Pretty sure he’ll have a very good read. Many thanks for sharing!