我的 Swift-iOS 应用旨在显示用户在 map 上的位置。但是,XCode 调试控制台告诉我需要请求权限才能显示用户的位置。我想,我这样做了,但对话从未出现。
这是错误消息,在我调用 CLLocationManager::requestWhenInUseAuthorization()
错误:
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.
View Controller :
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
并将委托(delegate)设置为 self
,如 here 所示没有帮助。
最佳答案
从 iOS 8 开始,您必须调用其中一个请求...函数并将适当的条目添加到您的 Info.plist 文件中,NSLocationWhenInUseUsageDescription
或 NSLocationAlwaysUsageDescription
。
有关详细信息,请参阅引用资料 here
更新
确保
- map 以模拟位置为中心。
- 还要确保位置是模拟的。在 XCode(见图片)的调试区(下方)中执行此操作,或在
Debug > Location
下的模拟器中执行此操作。
调试区:
关于iOS 应用程序不要求位置权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24497928/