How to ask about android permission to react-native

How to ask about android permission to react-native:

Here we are going to discuss the “How to ask about android permission to react-native“, because without user permission you can’t able to access any android functionality.

Example image is like –

Introduction –

  • React native provide great permission accessing the library, so today we learn how to use this permission.
  • Always remember to put the permission accessing code within the page componentDidMount, so it’s ask about permission always on that page where it required.
  • Now we see to ask a different type of permission.

Firstly, we need to import the permission library on a page where you need to access the permission.

Step1:

Firstly we import the file.

import { StyleSheet, PermissionsAndroid } from 'react-native';

Step2: Asking About Device Location Permission

Now we ask about accessing device location permission in componentDidmount.

componentDidMount() 
{ 
try { 
const granted = PermissionsAndroid.request( 
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, 
{ 
title: 'App Title', 
message:'App Name needs access to your device location ', 
buttonNeutral: 'Ask Me Later', 
buttonNegative: 'Cancel',
 buttonPositive: 'OK',
},
) 
console.log('granted---------',granted) 
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the device location') 
} 
else{ 
console.log('device location permission denied'); 
} 
}catch (err){ console.warn(err); }

Step3: Asking About Contact Permission

Now we ask about accessing contact permission in componentDidmount.

componentDidMount() {
try {
const granted = PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.READ_CONTACTS,
{
title: 'App Title',
message:'App Name needs access to your Contact ',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
)
console.log('granted---------',granted)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can use the Contact')
}
else{
console.log('Contact permission denied');
}
}catch (err){
console.warn(err);
}

Step4: Asking About SMS Permission

Now we see one more example of accessing permission which sends SMS permission.

componentDidMount() {
try {
const granted = PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.SEND_SMS,//now you can understand that you need to change only
name of permission in this place
{
title: 'App Title',
message:'App Name needs access to your Contact ',
//buttonNeutral: 'Ask Me Later',//these buttons are optional you can use it according to your choice
//buttonNegative: 'Cancel',
//buttonPositive: 'OK',
},
)
console.log('granted---------',granted)
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('You can send the sms')
}
else{
console.log('send sms permission denied');
}
}catch (err){
console.warn(err);
}

So the tutorial is “How to ask about android permission to react-native“ completed.

You can find my post on medium as well click here please follow me on medium as well.

If have any query/issue, please feel free to ask.

Happy Coding Guys.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *