Hello, i’ll show you how easily to request all permission on android.
Installation
Initial our project with react-native
npx react-native init myapp
Request Permission Preparation
Before we start coding on .js file project. We need to write permission on AndroidManifest.xml first. you can find at myapp/android/app/src/main/AndroidManifest.xml
paste this code on inner manifest tag.
Let’s Coding
So, after we write permission that what we need.
now we can coding to request permission.
Delete all code on App.js
and paste this code
import React from 'react';
import {Button, Linking, PermissionsAndroid, ScrollView} from 'react-native';
const PERMISSIONS_TYPE = [
'READ_CALENDAR',
'WRITE_CALENDAR',
'CAMERA',
'READ_CONTACTS',
'WRITE_CONTACTS',
'GET_ACCOUNTS',
'ACCESS_FINE_LOCATION',
'ACCESS_COARSE_LOCATION',
'ACCESS_BACKGROUND_LOCATION',
'RECORD_AUDIO',
'READ_PHONE_STATE',
'CALL_PHONE',
'READ_CALL_LOG',
'WRITE_CALL_LOG',
'ADD_VOICEMAIL',
'USE_SIP',
'PROCESS_OUTGOING_CALLS',
'BODY_SENSORS',
'SEND_SMS',
'RECEIVE_SMS',
'READ_SMS',
'RECEIVE_WAP_PUSH',
'RECEIVE_MMS',
'READ_EXTERNAL_STORAGE',
'WRITE_EXTERNAL_STORAGE',
'BLUETOOTH_CONNECT',
'BLUETOOTH_SCAN',
'BLUETOOTH_ADVERTISE',
'ACCESS_MEDIA_LOCATION',
'ACCEPT_HANDOVER',
'ACTIVITY_RECOGNITION',
'ANSWER_PHONE_CALLS',
'READ_PHONE_NUMBERS',
'UWB_RANGING',
];
async function requestPermission(PERMISSION_TYPE) {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS[PERMISSION_TYPE],
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log(`You can use ${PERMISSION_TYPE}`);
} else {
console.log(`${PERMISSION_TYPE} permission denied`);
}
} catch (err) {
console.warn(err);
}
}
export default function App() {
return (
{PERMISSIONS_TYPE.map((TYPE, index) => (
);
}
Save it and runnning the app using terminal
npm run android
Result
The result will look like this :
and if we click a button and grant or denied. The information will appear on your terminal:
that’s it! ask me if you have an question
Source Reference: https://reactnative.dev/docs/permissionsandroid