Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS app doesn't ask for location permission

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.

like image 834
Unapiedra Avatar asked Jun 30 '14 19:06

Unapiedra


People also ask

How do you get an app to ask your location?

To do so, head to Settings > Privacy > Location Services. You'll see a list of apps and their location permission settings. Tap an app's name here to change its location permissions. You can then select “Ask Next Time,” if you like.

How do I allow an app to access my location on iPhone?

You can turn Location Services on or off at Settings > Privacy > Location Services. You can turn Location Services on either during the Setup Assistant process or later through the Location Services setting. You can individually control which apps and system services have access to Location Services data.

How do I always allow location in IOS?

Go to Settings > Privacy, then select Location Services. Select an app, then turn Precise Location on or off.

Why can't I allow location on my iPhone?

Turn on Location Services and Location Access for Maps. In the Settings app, tap Privacy, then tap Location Services. Make sure Location Services is on, and make sure Maps is set to While Using the App or Widgets. Set the date, time, and time zone correctly on your device.


1 Answers

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

like image 174
Cayke Prudente Avatar answered Sep 24 '22 11:09

Cayke Prudente