我试图在 iOS 8 上使用 MapKit,但我一直收到错误:
Trying to start MapKit location updates without prompting for location authorization. Must call
-[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager
requestAlwaysAuthorization] first.
在这里查找,我发现我必须在我的 plist 中实现 NSLocationWhenInUsageDescription
并调用 locationManager.requestWhenInUseAuthorization()
但什么也没有发生,我仍然在控制台中得到该错误。我做错了什么?
最佳答案
在我的应用程序委托(delegate)中,我为类外的 locationManager 创建了一个可选的 var,然后设置
locManager = CLLocationManager()
locManager!.requestWhenInUseAuthorization()
这会导致警报 View 弹出 NSLocationWhenInUseUsageDescription 或 NSLocationAlwaysUsageDescription 如果你适本地改变它。
然后在 View Controller 文件中,我在类外创建了另一个 var 来保存本地 CLLocationManager。然后我设置
if locManager {
locMan = locManager!
locMan!.delegate = self
}
然后就可以使用delegate方法了
func locationManager(_manager: CLLocationManager!,didChangeAuthorizationStatus status: CLAuthorizationStatus)
当授权状态改变时调用,当用户响应弹出窗口时调用。在此内部,您可以使用这段代码将用户位置放在 map 上
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
map.showsUserLocation = true
}
只有在您被授权使用时才会将用户位置添加到 map 中。
关于ios - iOS 8 和 Swift 中的 MapKit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24105896/