7

I'm trying to use MapKit on iOS 8 and I keep getting the error:

Trying to start MapKit location updates without prompting for location authorization. Must call   
-[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager 
requestAlwaysAuthorization] first. 

Looking it up here, I found that I had to implement NSLocationWhenInUsageDescription in my plist and also make a call to locationManager.requestWhenInUseAuthorization() but nothing happens and I still get that error in the console. What am I doing wrong?

Phil
  • 205
  • 2
  • 4
  • 13
  • 1
    `NSLocationWhenInUsageDescription` is the wrong key. Use `NSLocationWhenInUseUsageDescription`. – Klaas Oct 15 '14 at 13:08

3 Answers3

10

In my application delegate I made an optional var for the locationManager outside the class and then set

locManager = CLLocationManager()
locManager!.requestWhenInUseAuthorization()

This causes the alert view to pop up with your NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription if you change it appropriately.

Then in the view controller file I made another var outside the class to hold a local CLLocationManager. I then set

if locManager {
        locMan = locManager!
        locMan!.delegate = self
    }

Then you can use the delegate method

func locationManager(_manager: CLLocationManager!,didChangeAuthorizationStatus status: CLAuthorizationStatus)

which gets called when the authorisation status changes, which it does when the user responds to the pop up. Inside this you can use this bit of code to put the user location on the map

if status == CLAuthorizationStatus.AuthorizedWhenInUse {
        map.showsUserLocation = true
    }

will add the users location to the map only if you are authorised for when in use.

Pang
  • 9,564
  • 146
  • 81
  • 122
Robert Mcc
  • 116
  • 1
  • 5
  • That got rid of the error but it still doesn't request permission or show the user's location. – Phil Jun 08 '14 at 21:53
  • I think it only prompts you the first time you open the app so you either have to delete from the simulator or device or go into the settings and allow location – Robert Mcc Jun 09 '14 at 11:55
  • That didn't work and the app doesn't even show up under location settings. – Phil Jun 09 '14 at 20:17
  • In my AppDelegate.swift i have the following: `import CoreLocation var locManager: CLLocationManager? @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate{ var window: UIWindow? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool { // Override point for customization after application launch. locManager = CLLocationManager() locManager!.requestWhenInUseAuthorization() return true }` – Robert Mcc Jun 10 '14 at 22:17
  • And in the view controller: `var locMan: CLLocationManager? class ViewController: UIViewController, CLLocationManagerDelegate { @IBOutlet var map : MKMapView override func viewDidLoad() { super.viewDidLoad() if locManager { locMan = locManager! locMan!.delegate = self } } func locationManager(_manager: CLLocationManager!, didUpdateLocations locations: AnyObject[]!){ self.map.showsUserLocation = true }` – Robert Mcc Jun 10 '14 at 22:17
  • and then the method that checks if the authorisation has changed in view controller: `func locationManager(_manager: CLLocationManager!,didChangeAuthorizationStatus status: CLAuthorizationStatus) { if status == CLAuthorizationStatus.AuthorizedWhenInUse { map.showsUserLocation = true } }` – Robert Mcc Jun 10 '14 at 22:22
  • That didn't work in my project nor in a totally blank project. Not sure what's wrong... – Phil Jun 10 '14 at 23:37
  • @Phil could you provide a link to your (blank) project so that we can have a try and get a chance to fix it? – Stéphane de Luca Jun 12 '14 at 16:16
  • Hello guys, I found this repo with the other approach for locating the user and the output has the same error, I've being trying to have a working example for locating a user with MapKit for all week. Cheers, https://github.com/orlangur87/MapKit-Using – antonio-gomez Jun 12 '14 at 18:35
  • I was facing this problem,Now the popup is coming .But the problem is the popup disappears after 1/2 seconds.It does not take any input form user.Can you help me with this?I'm using objective c. – Rabby Alam Sep 18 '14 at 05:54
  • The key information is a variable outside the method focus. When I made a property, the alert box showed up. When the locationManager is created as a local variable inside a function, it is probably already deallocated when the alertBox is ready to show. – auco Jun 13 '15 at 23:09
9

I have been bothered by that one as well, until I realized that the info.plist key has changed. If you had NSLocationUsageDescription in, you will need to change to either NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription (set at least one to YES), it is now mandatory in iOS8.

And then, Robert's code works, as it should (thanks for sharing).

  • 1
    Just a note: These keys aren't YES/NO. You're supposed to put text that gives a reason that the user will see that explains why you need location (eg. "The app finds places of interest near you."). So putting "YES" works but so will "NO" or "42" as long as some value is set for the key. –  Aug 08 '14 at 11:19
7

It is NSLocationWhenInUseUsageDescription, not NSLocationWhenInUsageDescription. Most places online have the wrong key

Jonathan Brown
  • 3,644
  • 3
  • 25
  • 31
  • 1
    Thanks GOD! Spent a full day trying to figure out why nothing works and indeed it looks like many posts have got it wrong. thanks!! – TommyG Mar 21 '15 at 20:35