Marcus Wayne Marcus Wayne - 4 months ago 10
iOS Question

How to execute a task after location permission granted ios

I'm trying to set up an on boarding where I'm asking the user for a few permissions including: Location, Notifications and Camera. I have 3 different View Controllers set up, each asking for one of the permissions and explaining why. On each of the view controllers I have a button at the bottom that says "Grant Permission".

When the user clicks the button I want the permission dialogue to pop up, and once the user clicks allow I want to transition to the next view controller.

Here is what I have right now:

class OnboardingStep2:UIViewController{

override func viewDidLoad() {
self.view.backgroundColor = StyleKit.orangeWhite()
}

@IBAction func getPermission(sender: AnyObject) {

dispatch_sync(dispatch_get_main_queue()) {
let locManager = CLLocationManager()
locManager.requestAlwaysAuthorization()

}

if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized) {
self.performSegueWithIdentifier("goToStep3", sender: self)
}

}
}


I've tried using dispatch to queue up the tasks, but when using async the permission dialogue pops up and then immediately it closes because the authorization check is run (I'm assuming). Using dispatch_sync, the dialogue is never shown.

What is the best way to do this, I want the permission dialogue to pop up first and once the user clicks allow i want to segue.

Answer

Conform to the CLLocationManagerDelegate

Then call this:

func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    switch status {
    case .NotDetermined:
        manager.requestLocation()
    case .AuthorizedAlways, .AuthorizedWhenInUse:
        // Do your thing here
    default:
        // Permission denied, do something else
    }
}
Comments