4

I have a button that when tap on it I have to check microphone permission.

For this reason I done this:

public func askMicrophoneAuthorization()
    {

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

                    } else
                    {
                        self.denied()
                    }
                }
            }
 }

my problem is this: when I tap on button and askMicrophoneAuthorization method is called, if its the first time that I ask the permission, the microphone system alert, with text inserted in plist file, shows and I can denied or not the permission. If I deny the permission and then I re-tap on button method self.denied() is executed and I dont see the microphone system alert. Is it possibile to re-show the system-alert?

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161
gianni rodari
  • 117
  • 3
  • 7
  • 1
    in this case user needs to go to the Settings and manually permit recording. This is the way the system works. It thinks that an app should not disturb user with alerts if user has made their choice once – Andrey Chernukha May 31 '19 at 14:16
  • As an aside, while it’s unlikely to ever be a problem, as a general rule I would not recommend `unowned` in conjunction with asynchronous method. What if `self` was dismissed/deallocated in the intervening time? Use `[weak self]` with `self?.goToNextStep()` and `self?.denied()`... – Rob May 31 '19 at 16:47

1 Answers1

3

It's not possible to show the system alert if the user has already denied. The best you can do is to check the permission and if they're denied show an alert with a button that opens the app settings.

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()
    }
}
Rico Crescenzio
  • 3,952
  • 1
  • 14
  • 28