ioscore-bluetoothkey-value-observing

Is it possible to detect when the user answers the Bluetooth permission request on iOS?


Most of the permission requests on iOS these days have an API with a callback for when the user answers the request alert. Is it possible to get a similar effect for the Bluetooth permission request? It would be nice to be able to have our app respond to the user answering the alert.

I've tried using KVO, but my observer method doesn't get called:

private static var obsContext = 0

...

if CBCentralManager.authorization == .notDetermined {
  CBCentralManager.addObserver(self, forKeyPath: "authorization", context: &MyClass.obsContext)
}

...

@objc override func observeValue(forKeyPath keyPath: String?, of object: Any?,
                                 change: [NSKeyValueChangeKey: Any]?,
                                 context: UnsafeMutableRawPointer?) {
  if context == &MyClass.obsContext {
    ...
  }
}

Am I doing something wrong? Is there another way to do this? Or is it just not possible for Bluetooth?


Solution

  • Paulw11's suggestion worked for me. If I watch for the UIApplication.didBecomeActiveNotification notification, I can detect when the permission request dialog is dismissed. For safety, I'm also watching for UIApplication.didEnterBackgroundNotification in case the user puts the device to sleep or otherwise exits the app without responding to the dialog.