ios 如何在用户拒绝一次后再次请求权限?

zd287kbt  于 2023-01-03  发布在  iOS
关注(0)|答案(1)|浏览(289)

目前,如果用户没有拒绝授予权限,我可以使用以下代码请求对麦克风的权限:

permission = await this.diagnostic.isMicrophoneAuthorized();
                if (!permission) {
                    permission = await this.diagnostic.requestMicrophoneAuthorization()
                    .then(async () => {
                        console.log('this.diagnostic.requestMicrophoneAuthorization succeed.');
                        return await this.diagnostic.isMicrophoneAuthorized();
                    })
                    .catch((err) => {
                        console.error('this.diagnostic.requestMicrophoneAuthorization failed, error: ', err);
                        return false;
                    })
                }

但是一旦它们拒绝了请求,requestMicrophoneAuthorization仍然被触发,但是没有对话框弹出,并且结果总是返回false。
有没有办法用Ionic 3再次请求授权?

zqdjd7g9

zqdjd7g91#

为此,您需要使用cordova-diagnostic-plugin检查当前的麦克风授权状态,如果授权被拒绝,则再次请求授权。

import { Diagnostic } from '@ionic-native/diagnostic/ngx';

constructor(private diagnostic: Diagnostic) {}

    async requestMicrophoneAuthorization() {
// Check the current microphone authorization status
let authorizationStatus = await this.diagnostic.getMicrophoneAuthorizationStatus();
    if (authorizationStatus === this.diagnostic.permissionStatus.DENIED_ALWAYS) {
    // If the authorization has been denied always, we need to ask the user to grant
    // microphone permission again.
    try {
    // Request microphone authorization
        authorizationStatus = await this.diagnostic.requestMicrophoneAuthorization();
    console.log('Microphone authorization status: ', authorizationStatus);
    } catch (error) {
        console.error('Error requesting microphone authorization: ', error);
    }
   }
  }

相关问题