用到的三方库:react-native-permissions
import { Platform } from 'react-native'
import { check, checkMultiple, PERMISSIONS, request, requestMultiple, RESULTS } from 'react-native-permissions'
/**
* 单个权限检查
* @param {object} permission
* {
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
android: {
type: PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
title: '请求获取模糊的定位权限',
message: '仅用于模糊标识'
}
}
* @returns RESULTS
*/
export const checkPermission = async permission => {
const platform = Platform.select(permission)
if (Platform.OS === 'android') {
const result = await check(platform.type)
if (result === RESULTS.DENIED) {
return await request(platform.type, {
title: platform.title,
message: platform.message,
buttonPositive: '好的',
buttonNegative: '拒绝'
})
}
return result
} else {
if (!platform) return RESULTS.GRANTED
const result = await check(platform)
if (result === RESULTS.DENIED) {
return await request(platform)
}
return result
}
}
/**
* 同时检查多个权限
* @param {*} permissions
* {
ios: [PERMISSIONS.IOS.LOCATION_WHEN_IN_USE, PERMISSIONS.IOS.CAMERA],
android: {
types: [PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION, PERMISSIONS.ANDROID.CAMERA],
title: 'xxxx',
message: 'xxxxxxxxx'
}
}
*/
export const checkMultiplePermissions = async permissions => {
const platform = Platform.select(permissions)
if (Platform.OS === 'android') {
const permissionTypes = platform.types
const result = await checkMultiple(permissionTypes)
const deniedTypes = []
for (const pt of permissionTypes) {
if (result[pt] === RESULTS.DENIED) {
deniedTypes.push(pt)
}
}
if (!deniedTypes.length) return result
return {
...result,
...(await requestMultiple(deniedTypes, {
title: platform.title,
message: platform.message,
buttonPositive: '好的',
buttonNegative: '拒绝'
}))
}
} else {
const result = await checkMultiple(platform)
const deniedTypes = []
for (const pt of platform) {
if (result[pt] === RESULTS.DENIED) {
deniedTypes.push(pt)
}
}
if (!deniedTypes.length) return result
return {
...result,
...(await requestMultiple(deniedTypes))
}
}
}
/**
* 检查是否有使用用户定位的权限
* @returns RESULTS
*/
export const checkLocationPermission = async () =>
checkPermission({
ios: PERMISSIONS.IOS.LOCATION_WHEN_IN_USE,
android: {
type: PERMISSIONS.ANDROID.ACCESS_COARSE_LOCATION,
title: '请求获取模糊的定位权限',
message: '仅用于模糊标识'
}
})
/**
* 检查是否有读取相册的权限
* @returns RESULTS
*/
export const checkReadFromLibraryPermission = async title => {
const type =
Platform.Version >= 33 ? PERMISSIONS.ANDROID.READ_MEDIA_IMAGES : PERMISSIONS.ANDROID.READ_EXTERNAL_STORAGE
return checkPermission({
ios: PERMISSIONS.IOS.PHOTO_LIBRARY,
android: { type, title: title || '更换头像/意见反馈等, 需要相册权限' }
})
}
/**
* 检查是否有写入相册的权限
* @returns RESULTS
*/
export const checkWriteToLibraryPermission = async title =>
await checkMultiplePermissions({
ios: [PERMISSIONS.IOS.PHOTO_LIBRARY],
android: {
types: [PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE],
title: title || '下载推广资源, 需要相册的写入权限'
}
})
/**
* 检查是否有使用相机权限
* @returns RESULTS
*/
export const checkCameraPermission = async title =>
checkMultiplePermissions({
ios: [PERMISSIONS.IOS.CAMERA],
android: {
types: [PERMISSIONS.ANDROID.CAMERA, PERMISSIONS.ANDROID.WRITE_EXTERNAL_STORAGE],
title: title || '更换头像/意见反馈等, 需要相机权限'
}
})
网友评论