Created
January 24, 2020 13:09
-
-
Save antoinerousseau/34d871ae084003c03da9b3047a9fdba9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PermissionsAndroid, Platform, Alert, Permission, PermissionStatus } from "react-native" | |
const permissions: Record<string, Permission> = { | |
// location: PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, | |
camera: PermissionsAndroid.PERMISSIONS.CAMERA, | |
// read: PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE, | |
write: PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, | |
} | |
export const getPermissions = async (keys: string[]) => { | |
if (Platform.Version < 23) { | |
return true | |
} | |
const checks = keys.map((key: string) => PermissionsAndroid.check(permissions[key])) | |
const current = await Promise.all(checks) | |
const toAsk: Permission[] = [] | |
current.forEach((result, index) => { | |
if (!result) { | |
toAsk.push(permissions[keys[index]]) | |
} | |
}) | |
const results: Record<string, PermissionStatus> = await PermissionsAndroid.requestMultiple(toAsk) | |
const rejected: string[] = [] | |
Object.keys(results).forEach((permissionName: string) => { | |
if (results[permissionName] === PermissionsAndroid.RESULTS.DENIED) { | |
rejected.push(`${permissionName} refusée par l’utilisateur`) | |
} else if (results[permissionName] === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) { | |
rejected.push(`${permissionName} bloquée par l’utilisateur`) | |
} | |
}) | |
if (rejected.length > 0) { | |
Alert.alert("Permissions manquantes", rejected.join("\n")) | |
return false | |
} | |
return true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const getPermissions = async (_keys: string[]) => true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment