Skip to content

Instantly share code, notes, and snippets.

@antoinerousseau
Created January 24, 2020 13:09
Show Gist options
  • Save antoinerousseau/34d871ae084003c03da9b3047a9fdba9 to your computer and use it in GitHub Desktop.
Save antoinerousseau/34d871ae084003c03da9b3047a9fdba9 to your computer and use it in GitHub Desktop.
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
}
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