0

Having some issues with my auth request method shown below:

    func requestAuthorisation() {
        if CLLocationManager.authorizationStatus() == .NotDetermined {
            let locationPermissionRequest = UIAlertController.init(title: "Location services", message:  "Some message, hidden for obvious reasons...", preferredStyle: .Alert)
            let alwaysOnButton = UIAlertAction.init(title: "Always on (recommended)", style: .Default, handler: { (alert: UIAlertAction!) in
                locationPermissionRequest.dismissViewControllerAnimated(true, completion: {
                    self.locationManager.requestAlwaysAuthorization()
                })
            })
            let backgroundButton = UIAlertAction.init(title: "On when in use", style: .Default, handler: { (alert: UIAlertAction!) in
                locationPermissionRequest.dismissViewControllerAnimated(true, completion: {
                    self.locationManager.requestWhenInUseAuthorization()
                })
            })
            let noButton = UIAlertAction.init(title: "Off", style: .Destructive, handler: { (alert: UIAlertAction!) in
                locationPermissionRequest.dismissViewControllerAnimated(true, completion: nil)
            })
            locationPermissionRequest.addActions(withActions: [alwaysOnButton, backgroundButton, noButton])
            locationPermissionRequest.present(true, completion: nil)
        }
    }

The problem here is that when I tap the when in use button or the always button, their actions are invoked and the alert is dismissed but the location auth is never requested. I have put a breakpoint on the lines where the auth request is made and the methods are being invoked. Interestingly if I move the call to the auth methods out of their respective completion blocks then it works fine, but for my purposes I need them to not be invoked until my custom alert is dismissed.

Does anyone know why I am seeing this behaviour/know of a fix?

EDIT:

I just tried wrapping in a call to dispatch on the main thread as I had a sneaking suspicion it was a threading issue but alas, it is not.

Jacob King
  • 6,025
  • 4
  • 27
  • 45

1 Answers1

0

I know this is quite an old question, but I ran into this issue earlier and wanted to suggest what fixed it for me.

The alert controller is tied to the location manager CLLocationManager object. If you aren't keeping that object around, then the auth alert controller will either not show or disappear before the user can tap on it.

I'm not sure if that was/is you're issue with the code above, as I'm not sure what scope the locationManager really is, but this solved my issue with similar symptoms.

Baza207
  • 2,123
  • 1
  • 22
  • 40