我正在开发一段针对iOS 8的Swift代码,我试图做与位置相关的事情,因此我在我的Swift视图控制器文件中实现了以下内容:
let locationManger:CLLocationManager = 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文件,但它没有起作用。顺便说一下,如果我在模拟器的隐私设置中选择“始终”,那么如果我立即切换回应用程序,它就可以工作。有人能帮忙吗?我确定这是问题所在,因为我的控制台上显示“未经授权”。任何帮助?
locationManager.requestWhenInUseAuthorization()
,而不仅仅是在你的状态未经授权时。 - Paulw11