簡體   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