Skip to content

Instantly share code, notes, and snippets.

@sturmenta
Created October 16, 2019 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturmenta/e693385cf1598dbee149f16c940aac10 to your computer and use it in GitHub Desktop.
Save sturmenta/e693385cf1598dbee149f16c940aac10 to your computer and use it in GitHub Desktop.
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