10

My Swift-iOS app is meant to show the user's location on a map. However, the XCode debug console tells me I need to ask permission to show the user's location. I think, I do that but the dialog never comes up.

Here is the error message, and below the ViewController where I call CLLocationManager::requestWhenInUseAuthorization()

Error:

2014-06-30 21:25:13.927 RowingTracker2[17642:1608253] Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

ViewController:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate {
    @IBOutlet var mapview: MKMapView = nil
    var locationmgr : CLLocationManager!
                            
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        locationmgr = CLLocationManager()
        locationmgr.requestWhenInUseAuthorization()
        mapview.showsUserLocation = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

How do I request authorization to use the location? You can find the complete project here.(Commit)

Info

Even making ViewController inherit from CLLocationManagerDelegate and setting the delegate to self as indicated here doesn't help.

Community
  • 1
  • 1
Unapiedra
  • 15,037
  • 12
  • 64
  • 93

4 Answers4

26

As of iOS 8 you have to call one of the request... functions and add the appropriate entry to your Info.plist file, either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription.

For more information, see the reference here

Update

Make sure that

  1. the map is centered on the simulated location.
  2. Also make sure that a location is simulated. Either do so in the Debug Area (down below) of XCode (see image), or do it in the simulator under Debug > Location.

Debug Area: Location Simulation in the debug area

Unapiedra
  • 15,037
  • 12
  • 64
  • 93
David Berry
  • 40,941
  • 12
  • 84
  • 95
  • Now, it asks for permission but still no user location is shown. New question, or can this be added to this question? – Unapiedra Jun 30 '14 at 20:58
  • what environment are you testing in? – David Berry Jun 30 '14 at 22:46
  • OS X Maverics with XCode 6-Beta2 and the Simulator. Target Platform 7.1. I'll have another go at it tonight. – Unapiedra Jul 02 '14 at 08:55
  • 1
    In the simulator you have to enable location simulation in the scheme settings or in the toolbar at the bottom of the debug window. – David Berry Jul 02 '14 at 15:38
  • It was enabled in the simulator (there is no "scheme settings" I could find) but not at the bottom of the debug window. Also it was centred on my country (Germany) and once I selected "London" it didn't automatically center (I know how to let it do that). – Unapiedra Jul 02 '14 at 23:33
  • Now I suspect that not-being-centered was the problem all along. – Unapiedra Jul 02 '14 at 23:40
  • The location doesn't seem to dynamically update when you change it in the debug menu. You have to make sure you set it before the map is opened then things should be ok. On one of the scheme settings pages there's a way to select a GPS file to use for location. – David Berry Jul 03 '14 at 14:33
  • I know. If you change it in the debug menu (except using a GPX file), the location in the simulator becomes "Custom". Best is to select it in the simulator directly. – Unapiedra Jul 03 '14 at 14:34
8

You need to use requestWhenInUseAuthorization and also need to create a value at yourapp-Info.plist named NSLocationWhenInUseUsageDescription

Cayke Prudente
  • 220
  • 3
  • 5
  • Crazy, but this seemed to do the job for me. Not sure how you figured this out, but very nice find... I spend hours trying to figure this one out! – jengelsma Jun 04 '15 at 17:22
  • It seems like this is a must now. I forgot to put this too and the app wouldn't ask for location permission. – tyegah123 Nov 02 '15 at 07:25
2

I use NSLocationAlwaysUsageDescription with the value as the text that will pop up when asking for permission, such as

"I would like permission to spy on you 24/7"

I would also add NSLocationWhenInUseUsageDescription with the value as the message.

fredmaggiowski
  • 2,232
  • 3
  • 25
  • 44
0

As David Berry, Cayke Prudente and Levi Johnson mentioned, I just needed to add NSLocationAlwaysUsageDescription to my Info.plist file. To understand more WHY I needed this, I went for further documentation and I'm sharing here, as it can help others the same way they helped me.

The user prompt contains the text from the NSLocationWhenInUseUsageDescription key in your app’s Info.plist file, and the presence of that key is required when calling this method.

https://developer.apple.com/documentation/corelocation/cllocationmanager/1620562-requestwheninuseauthorization

finx
  • 1,293
  • 9
  • 9