thank_you
thank_you

Reputation: 11107

Can't Determine When User Doesn't Allow Location Services

I'm requesting the user to turn on location services. I want to know when a user clicks Don't Allow so I can handle some notifications. However, the didFailWithError or didChangeAuthorizationStatus methods are not being called when I click Don't Allow. I know this nothing is printed in the logger. I've attached a code sample. What am I doing wrong and how do I fix this. Thanks.

import UIKit

import CoreLocation

class AwesomeViewController: UIViewController, CLLocationManagerDelegate {

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        let authorizationStatus = CLLocationManager.authorizationStatus()

        if(authorizationStatus == .AuthorizedWhenInUse || authorizationStatus == .AuthorizedAlways) {
            // authorization is good
        } else {
            locationManager.requestWhenInUseAuthorization()
        }
    }


    func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        print(status)
    }

    func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {            
        print(error.localizedDescription)
    }
}

Upvotes: 0

Views: 210

Answers (1)

matt
matt

Reputation: 535121

the didFailWithError or didChangeAuthorizationStatus methods are not being called

Those are delegate methods. Your location manager does not have any delegate - certainly it does not have you (the AwesomeViewController instance) as a delegate. So it is not going to call those methods, ever. You need to set the location manager's delegate (in this case you would set it to self).

Upvotes: 1

Related Questions