- PermissionsAndroid
- Project with Native Code Required
- Example
- Permissions that require prompting the user
- Result strings for requesting permissions
- Reference
- Methods
- constructor()
- check()
- request()
- requestMultiple()
- React Native PermissionsAndroid | Ask Run Time Android Permission
- What is Android Permission?
- Why We have to Ask for Permission?
- How to Ask for Permission in Android?
- Run Time Permissions that Requires Confirmation from the User
- How to Ask Run Time Android Permission using React Native PermissionsAndroid?
- Example to Ask for the Run Time Permission
- To Make a React Native App
- App.js
- To Run the React Native App
- 2 thoughts on “React Native PermissionsAndroid | Ask Run Time Android Permission”
- Leave a Comment Cancel reply
PermissionsAndroid
Project with Native Code Required
The following section only applies to projects with native code exposed. If you are using the managed expo-cli workflow, see the guide on Permissions in the Expo documentation for the appropriate alternative.
PermissionsAndroid provides access to Android M’s new permissions model. The so-called «normal» permissions are granted by default when the application is installed as long as they appear in AndroidManifest.xml . However, «dangerous» permissions require a dialog prompt. You should use this module for those permissions.
On devices before SDK version 23, the permissions are automatically granted if they appear in the manifest, so check should always result to true and request should always resolve to PermissionsAndroid.RESULTS.GRANTED .
If a user has previously turned off a permission that you prompt for, the OS will advise your app to show a rationale for needing the permission. The optional rationale argument will show a dialog prompt only if necessary — otherwise the normal permission prompt will appear.
Example
- Function Component
- Class Component
Permissions that require prompting the user
Available as constants under PermissionsAndroid.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’
- ACCESS_BACKGROUND_LOCATION : ‘android.permission.ACCESS_BACKGROUND_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.permission.ADD_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’
- BLUETOOTH_CONNECT : ‘android.permission.BLUETOOTH_CONNECT’
- BLUETOOTH_SCAN : ‘android.permission.BLUETOOTH_SCAN’
- BLUETOOTH_ADVERTISE : ‘android.permission.BLUETOOTH_ADVERTISE’
Result strings for requesting permissions
Available as constants under PermissionsAndroid.RESULTS :
- GRANTED : ‘granted’
- DENIED : ‘denied’
- NEVER_ASK_AGAIN : ‘never_ask_again’
Reference
Methods
constructor()
check()
Returns a promise resolving to a boolean value as to whether the specified permissions has been granted.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
permission | string | Yes | The permission to check for. |
request()
Prompts the user to enable a permission and returns a promise resolving to a string value (see result strings above) indicating whether the user allowed or denied the request or does not want to be asked again.
If rationale is provided, this function checks with the OS whether it is necessary to show a dialog explaining why the permission is needed (https://developer.android.com/training/permissions/requesting.html#explain) and then shows the system permission dialog.
Parameters:
Name | Type | Required | Description |
---|---|---|---|
permission | string | Yes | The permission to request. |
rationale | object | No | See rationale below. |
Rationale:
Name | Type | Required | Description |
---|---|---|---|
title | string | Yes | The title of the dialog. |
message | string | Yes | The message of the dialog. |
buttonPositive | string | Yes | The text of the positive button. |
buttonNegative | string | No | The text of the negative button. |
buttonNeutral | string | No | The text of the neutral button. |
requestMultiple()
Prompts the user to enable multiple permissions in the same dialog and returns an object with the permissions as keys and strings as values (see result strings above) indicating whether the user allowed or denied the request or does not want to be asked again.
Источник
React Native PermissionsAndroid | Ask Run Time Android Permission
The post, Ask Run Time Android Permission using React Native PermissionsAndroid includes
- What is Android Permission?
- Why we have to ask for permission?
- How to ask for Permission in Android?
- How to ask run time Permission in React Native?
- Example to ask for the run time permission.
So Let’s get started.
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, as well as certain system features such as camera and internet. So they introduced Android Permission feature.
In Android permission modal every applications which is using sensitive data or some certain system features have to ask for the permissions from user.
The purpose of permission is to protect the privacy of an Android user.
Why We have to Ask for Permission?
Android applies the permission modal because some of the 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 to solve this sensitive issue application have to ask for permission to access those sensitive data.
How to Ask for Permission in Android?
Now you have an idea about the Android permission, let’s see how to ask for the permission in Android.
To ask for permission in Android you have to follow two steps:
1. You have to add those permissions requests in project -> app -> src -> AndroidManifest.xml file.
For Example, if we want to ask for the Camera Permission we have to add the following permission request in AndroidManifest.xml
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 you have to ask runtime permission as well. If you are targeting the Android version dangerous permissions in run time (You don’t have to ask for all the permission just some permission which are called dangerous permissions, you can see the list below).
According to the official docs, dangerous permissions require a dialog prompt.
So whenever you are working with this dangerous permissions you need to check whether the permission is granted by the user or not 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.
Here is the list of dangerous permissions.
Run Time Permissions that Requires Confirmation from the User
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 |
Hope you have understood the run time permission now. Let’s have a look at how to ask for the Run Time permission in React Native.
How to Ask 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. You 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.
You can check the permission granted or not using PermissionsAndroid.RESULTS.GRANTED
To ask for the permission use
Result Strings for Requesting Permissions
Available 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 have an idea about Android permissions and components. Let’s move towards the Example which can help you to ask permission in your application.
Example to Ask for the Run Time Permission
In this example, we will ask for the camera permission which needs run time permission. We are making an button on the centre of the screen and on click of button we will ask for the run time permission for CAMERA. So let’s get started.
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install the react-native-cli command line utility. Open the terminal and go to the workspace and run
Run the following commands to create a new React Native project
If you want to start a new project with a specific React Native version, you can use the —version argument:
This will make a project structure with an index file named App.js in your project directory.
If you are using anything in your app that needs permission you need to add it in AndroidManifest.xml . For this example we are adding camera permission in AndroidManifest.xml.
Now jump into the project using
Open App.js in any code editor and replace the code with the following code.
App.js
Here PermissionsAndroid.request have two arguments:
- PermissionsAndroid.PERMISSIONS.CAMERA to generate a dialog to ask for CAMERA Permission.
- A JSON <‘title’:”,’message’:”>, which will help you to communicate or to show the dialog about the requirement of the permission if the user denied the permission. It will be generated like this.
To Run the React Native App
Open the terminal again and jump into your project using.
To run the project on an Android Virtual Device or on real debugging device
or on the iOS Simulator by running (macOS only)
That was the React Native Android Permission. If you have any doubt or you want to share something about the topic you can comment below or contact us here. The remaining components will be covered in the next article. Stay tuned!
Hope you liked it. 🙂
2 thoughts on “React Native PermissionsAndroid | Ask Run Time Android Permission”
Hi,
I am unable to show runtime access popup window in my android mobile. Could you please help me how to resolve this issue. Always it is showing error “never_ask_again” instead of showing popup window.
Can you please share which permission you are asking?
Leave a Comment Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Источник