优化官方推荐的uni.authorize文档中permission.js中的相册授权方法,判断是否有授权没有则弹窗请求授权。
发现很多小伙伴有这个需求,自己也在网上搜索了一番无果。
在此分享出来,方便大家开发。
// 判断相册权限是否开启 resolve()返回值可以根据需求更具体一点,在业务中拿到状态码后弹窗提示用户是否要去设置页修改权限
function judgeIosPermissionPhotoLibrary() {
return new Promise((resolve, reject) => {
var PHAuthorizationStatus = {
NotDetermined: 0, // 未申请过权限
Restricted: 1, // 拒绝
Denied: 2, // 拒绝
Authorized: 3, // 拥有
Limited: 4 // iOS 14 新增
};
if (!PHPhotoLibrary) {
var PHPhotoLibrary = plus.ios.import("PHPhotoLibrary");
}
var status = PHPhotoLibrary.authorizationStatus();
if (status === PHAuthorizationStatus.NotDetermined) {
PHPhotoLibrary.requestAuthorization(() => {
const status = PHPhotoLibrary.authorizationStatus(); {
if (status === PHAuthorizationStatus.Authorized) {
plus.ios.deleteObject(PHPhotoLibrary);
resolve(true)
} else {
plus.ios.deleteObject(PHPhotoLibrary);
resolve(false)
}
}
})
} else if (status === PHAuthorizationStatus.Authorized) {
plus.ios.deleteObject(PHPhotoLibrary);
resolve(true)
} else {
if (status === PHAuthorizationStatus.Denied || status === PHAuthorizationStatus.Restricted) {
console.log("您已拒绝访问相册权限,请在设置中手动开启。");
} else if (status === PHAuthorizationStatus.Limited) {
console.log("相册权限为有限访问。");
}
plus.ios.deleteObject(PHPhotoLibrary);
resolve(false)
}
})
}
0 个评论
要回复文章请先登录或注册