拒绝时显示麦克风权限

gia*_*ari 5 avfoundation ios swift ios-permissions

我有一个按钮,点击它时我必须检查麦克风权限。

为此,我这样做了:

public func askMicrophoneAuthorization()
    {

        recordingSession = AVAudioSession.sharedInstance()
        recordingSession.requestRecordPermission() { [unowned self] allowed in
                DispatchQueue.main.async {
                    if allowed
                    {
                        self.goToNextStep()

                    } else
                    {
                        self.denied()
                    }
                }
            }
 }
Run Code Online (Sandbox Code Playgroud)

我的问题是这样的:当我点击按钮并调用askMicrophoneAuthorization方法时,如果这是我第一次请求权限,麦克风系统警报(在plist文件中插入文本)会显示,我可以拒绝或不拒绝该权限。如果我拒绝该权限,然后重新点击按钮,方法 self.denied() 就会被执行,并且我看不到麦克风系统警报。是否可以重新显示系统警报?

Ric*_*zio 4

如果用户已经拒绝,则不可能显示系统警报。您能做的最好的事情就是检查权限,如果被拒绝,则会显示一条警报,并带有一个打开应用程序设置的按钮。

func askPermissionIfNeeded() {
    switch AVAudioSession.sharedInstance().recordPermission {
    case undetermined:
        askMicrophoneAuthorization()
    case denied:
        let alert = UIAlertController(title: "Error", message: "Please allow microphone usage from settings", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Open settings", style: .default, handler: { action in
            UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
        }))
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        present(alert, animated: true, completion: nil)
    case granted:
        goToNextStep()
    }
}
Run Code Online (Sandbox Code Playgroud)