Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

swift - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...