Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 245
Kashish Goel Avatar asked Jul 07 '16 18:07

Kashish Goel


1 Answers

Conform to the CLLocationManagerDelegate

Then call this:

Swift 3.0

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    switch status {
    case .notDetermined:
        manager.requestLocation()
    case .authorizedAlways, .authorizedWhenInUse:
        // Do your thing here
    default:
        // Permission denied, do something else
    }
}

Swift 2.2

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
    }
}
like image 155
CodeBender Avatar answered Nov 01 '22 09:11

CodeBender