场景描述(小程序)
我有这样一个需求,点击一个功能,需要用户授权手机的摄像头(等)权限,如果用户拒绝了,那么我们需要引导用户重新开启权限。
此报错的位置是在,当我们发现用户拒绝权限时,引导用户打开权限时报的错
需知
打开权限设置的API
是wx.openSetting()
openSetting 官方文档一句话
尝试把wx.openSetting()
放到showModal()
里代码
大概的检测流程如下
// 需求:引导用户手动开启摄像头的权限openSetting() {wx.showModal({title: '提示', //提示的标题,content: '请打开摄像头权限', //提示的内容,showCancel: true, //是否显示取消按钮,cancelText: '取消', //取消按钮的文字,默认为取消,最多 4 个字符,cancelColor: '#000000', //取消按钮的文字颜色,confirmText: '确定', //确定按钮的文字,默认为取消,最多 4 个字符,confirmColor: '#3CC51F', //确定按钮的文字颜色,success: res => {if (res.confirm) {// openSetting 是需要事件驱动的,保证它的同步性。wx.openSetting({success(res) {},fail(res) {}})} else if (res.cancel) {console.log('用户点击取消')wx.showToast({title: '需要摄像头权限',icon: 'none',duration: 2000,})}}});},wx.getSetting({let that = this;// 获取权限信息success: response => {if (!response.authSetting['scope.camera']) {wx.authorize({scope: 'scope.camera',success() {// 同意摄像头权限后的事务},fail() {// 不同意摄像头权限后,引导用户开启if (wx.openSetting) {// 如果没有授权摄像头权限,引导用户开启that.openSetting();} return;}})} else {// 同意摄像头权限后的事务}}})
参考文章
1. 微信小程序中用户拒绝授权的处理方式
2. 关于openSetting通过tap的调用