uniapp app获取相机权限,无权限时打开系统设置
async checkPermission() { let system = uni.getSystemInfoSync() if (system.platform === 'ios') { var result = false; var AVCaptureDevice = plus.ios.import("AVCaptureDevice"); var authStatus = AVCaptureDevice.authorizationStatusForMediaType('vide'); console.log("authStatus:" + authStatus); if (authStatus == 3 || authStatus == 0) { //authStatus == 0 时为初次打开相机 ios系统有默认授权弹窗 result = true; return result } else { uni.showModal({ title: '提示', content: '请打开相机权限', success(res) { if (res.confirm) { var UIApplication = plus.ios.import("UIApplication"); var application2 = UIApplication.sharedApplication(); var NSURL2 = plus.ios.import("NSURL"); var setting2 = NSURL2.URLWithString("app-settings:"); application2.openURL(setting2); plus.ios.deleteObject(setting2); plus.ios.deleteObject(NSURL2); plus.ios.deleteObject(application2); } } }); } plus.ios.deleteObject(AVCaptureDevice); return result; } else { plus.android.requestPermissions(['android.permission.CAMERA'], (e) => { if (e.deniedAlways.length > 0) { //权限被永久拒绝 // 弹出提示框解释为何需要权限,引导用户打开设置页面开启 uni.showModal({ title: '提示', content: '请打开手机相机功能(点击确定后在权限中授权相机功能)', // showCancel: false, // 不显示取消按钮 success(res) { if (res.confirm) { var Intent = plus.android.importClass("android.content.Intent"); var Settings = plus.android.importClass( "android.provider.Settings"); var Uri = plus.android.importClass("android.net.Uri"); var mainActivity = plus.android.runtimeMainActivity(); var intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); var uri = Uri.fromParts("package", mainActivity.getPackageName(), null); intent.setData(uri); mainActivity.startActivity(intent); } } }); } else if (e.deniedPresent.length > 0) { //权限被临时拒绝 // 弹出提示框解释为何需要权限,可再次调用plus.android.requestPermissions申请权限 uni.showModal({ title: '提示', content: '请打开手机相机功能(点击确定后在权限中授权相机功能)', // showCancel: false, // 不显示取消按钮 success(res) { if (res.confirm) { var Intent = plus.android.importClass("android.content.Intent"); var Settings = plus.android.importClass( "android.provider.Settings"); var Uri = plus.android.importClass("android.net.Uri"); var mainActivity = plus.android.runtimeMainActivity(); var intent = new Intent(); intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); var uri = Uri.fromParts("package", mainActivity.getPackageName(), null); intent.setData(uri); mainActivity.startActivity(intent); } } }); }else{ console.log('已有权限!'); return true } }, (e) => { // console.log(e); return true console.log('Request Permissions error:' + JSON.stringify(e)) }) } }
业务逻辑:点击拍照时授权,有权限返回true,无权限返回false且打开系统设置
老鸟也是从菜鸟开始的!