iOS应用不会要求位置许可 [英] iOS app doesn't ask for location permission
问题描述
我的Swift-iOS应用程序旨在显示用户在地图上的位置。但是,XCode调试控制台告诉我,我需要请求权限才能显示用户的位置。我想,我这样做但是对话框永远不会出现。
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.
这是错误信息,在ViewController下面我调用 CLLocationManager :: requestWhenInUseAuthorization()
Here is the error message, and below the ViewController where I call CLLocationManager::requestWhenInUseAuthorization()
错误:
2014 -06-30 21:25:13.927 RowingTracker2 [17642:1608253]试图在不提示位置授权的情况下启动MapKit位置更新。必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization]。
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:
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)
甚至使ViewController继承自 CLLocationManagerDelegate
并将委托设置为 self
如上所示此处无效。
Even making ViewController inherit from CLLocationManagerDelegate
and setting the delegate to self
as indicated here doesn't help.
推荐答案
从iOS 8开始,您必须调用其中一个请求...函数并将相应的条目添加到您的Info.plist文件中, NSLocationWhenInUseUsageDescription
或 NSLocationAlwaysUsageDescription
。
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
确保
- 地图以模拟位置为中心。
- 还要确保模拟某个位置。在XCode的调试区域(下面)中执行此操作(请参阅图像),或者在
Debug>下的模拟器中执行此操作。位置
。
- the map is centered on the simulated location.
- 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:
这篇关于iOS应用不会要求位置许可的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!