Search code examples
swiftauthorizationcllocationmanagerappdelegatecllocation

CLLocationManager enabled but checked Never


I am trying to determine in AppDelegate if user set location permissions for application to "Never". My app always crashing if not getting location.

my code in AppDelegate is:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    //location
    var manager = CLLocationManager()       
    if CLLocationManager.authorizationStatus() == .Restricted
            || .authorizationStatus() == .Denied {
        manager.requestWhenInUseAuthorization()
        manager.requestAlwaysAuthorization()
    }
}

Can anyone help me to resolve this issue?


Solution

  • The best article about CoreLocation and requesting authorization is here: http://nshipster.com/core-location-in-ios-8/

    A couple of points:

    1. Only request authorization if the current status is .NotDetermined
    2. If the current status is .Denied, then you can post an alert asking the user to go into settings and turn on location services, but calling either of the request methods will do nothing.
    3. Remember to put in a NSLocationUsageDescription in your plist! (I forget this routinely.)