【发布时间】:2014-08-21 06:50:41
【问题描述】:
我的 Swift-iOS 应用旨在在地图上显示用户的位置。但是,XCode 调试控制台告诉我需要请求权限才能显示用户的位置。我想,我这样做了,但对话永远不会出现。
这是错误消息,在我调用 CLLocationManager::requestWhenInUseAuthorization() 的 ViewController 下方
错误:
2014-06-30 21:25:13.927 RowingTracker2[17642:1608253] 尝试在不提示位置授权的情况下启动 MapKit 位置更新。必须先调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization]。
视图控制器:
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.
}
}
如何请求授权使用该位置? 你可以找到完整的项目here.(Commit)
信息
即使让 ViewController 从CLLocationManagerDelegate 继承并将委托设置为self,如here 所示也无济于事。
【问题讨论】:
标签: ios swift mapkit core-location