【发布时间】:2014-08-31 10:35:31
【问题描述】:
我正在为 iOS 8 编写一段 Swift 代码。我正在尝试做一些涉及位置的事情,因此我在我的 swift 视图控制器文件中实现了以下内容:
let locationManger:CLLocationManager = CLLocationManager()
var speedReceived:Double = 0
override func viewDidLoad() {
super.viewDidLoad()
locationManger.delegate = self
locationManger.desiredAccuracy = kCLLocationAccuracyBest
let authstate = CLLocationManager.authorizationStatus()
if(authstate == CLAuthorizationStatus.NotDetermined){
println("Not Authorised")
locationManger.requestWhenInUseAuthorization()
}
// Do any additional setup after loading the view, typically from a nib.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
var location:CLLocation = locations[locations.count - 1] as CLLocation
if(location.horizontalAccuracy > 0){
self.speedReceived = location.speed
println(self.speedReceived)
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("Couldn't get your location")
}
但是,我似乎无法让这段代码工作。它不会保存我对位置使用的偏好。它甚至没有提示我授予访问位置的许可。我已经尝试更新我的 info.plist。但它不起作用。顺便说一句,如果我总是在模拟器的隐私设置中选择,如果我立即切换回应用程序,它就会起作用。谁能帮忙?我确信这是问题所在,因为我在控制台上未获得授权。
有什么帮助吗?
【问题讨论】:
-
您说您已经更新了 info.plist - 您是否仔细检查了您的条目? “NSLocationWhenInUseUsageDescription”需要与描述您的位置使用情况的字符串一起出现。一个简单的错字将阻止您的应用提示位置访问。此外,您应该始终致电
locationManager.requestWhenInUseAuthorization(),而不仅仅是在您的身份未经授权时
标签: ios swift core-location cllocationmanager