Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
react-native ask permissions
requestLocationPermission({
grantPermission: () => console.warn('location granted'),
refusePermission: () => console.warn('location not granted'),
});
export const requestLocationPermission = ({ grantPermission, refusePermission }) => {
if (Platform.OS === 'android') {
try {
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, {
title: i18n.t('permissions.permissionToLocation.title'),
message: i18n.t('permissions.permissionToLocation.message'),
buttonNegative: i18n.t('permissions.permissionToLocation.buttonNegative'),
buttonPositive: i18n.t('permissions.permissionToLocation.buttonPositive'),
})
.then(granted => {
if (granted !== PermissionsAndroid.RESULTS.GRANTED) {
Alert.alert(
i18n.t('permissions.permissionToLocation.alert.title'),
i18n.t('permissions.permissionToLocation.alert.message'),
[
{
text: i18n.t('permissions.permissionToLocation.alert.button.askAgain'),
onPress: () => requestLocationPermission({ grantPermission, refusePermission }),
},
{
text: i18n.t('permissions.permissionToLocation.alert.button.continue'),
onPress: () => refusePermission(),
style: 'cancel',
},
],
{ cancelable: false },
);
} else grantPermission();
})
.catch(err => {
console.warn(err);
refusePermission();
});
} catch (err) {
console.warn(err);
refusePermission();
}
} else grantPermission();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment