1

Here is my code from a ViewController implementing CLLocationManagerDelegate:

func startLocationManager() {
    let locationManager = CLLocationManager()

    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters


    println("I'm called")


    locationManager.requestAlwaysAuthorization()
    // locationManager.startMonitoringSignificantLocationChanges()
    locationManager.startUpdatingLocation()

    let status = CLLocationManager.authorizationStatus()
    println(status.rawValue) // This print 0 which stands for kCLAuthorizationStatusNotDetermined

    println(CLLocationManager.locationServicesEnabled()) // true
}

func locationManager(manager: CLLocationManager!,
    didUpdateLocations locations: [AnyObject]!) {
    println("nobody call me ever, and I'm sad")
}

For some reason, I never get the prompt / alter to autorise location updates. I have tried on my device iOS 8.1 and the simulartor. I followed the advices found here: requestAlwaysAuthorization not showing permission alert : "Add Core Location framework to Project Settings / Targets / Capabilities / Background Modes set "Location Updates" and "Uses Bluetooth LE Accessories" Add key at Info.plist NSLocationAlwaysUsageDescription".

I have also tried to clean up and rebuild, nothing change. I feel clueless.

EDIT: This question seems related: iOS: App is not asking user's permission while installing the app. getting kCLAuthorizationStatusNotDetermined every time - Objective-c & Swift but the selected answer and its article doesn't expose anything new

Community
  • 1
  • 1
AsTeR
  • 7,247
  • 14
  • 60
  • 99
  • Did you set string for `NSLocationAlwaysUsageDescription` key in your app’s `Info.plist` file. If you don't, you won't see permission alert. – Rob Dec 22 '14 at 18:26
  • 1
    Yes I set a dummy string (and tried with it empty at the beginning). – AsTeR Dec 22 '14 at 20:43

1 Answers1

2

Your CLLocationManager object is local object and thus will be deallocated immediately after it falls out of scope. Make it a class property and then asynchronous processes like requesting authorization and determining the location will have a chance to run.

Rob
  • 415,655
  • 72
  • 787
  • 1,044