繁体   English   中英

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

[英]Ionic - How to request permission again after user rejected once?

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

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 再次请求授权?

为此,您需要使用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);
    }
   }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM