请问我运行的时候,locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)函数的代码没有执行,直接到locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)函数里,打印error信息:Error Domain=kCLErrorDomain Code=0 "The operation couldn’t be completed. (kCLErrorDomain error 0.)
3 回答
发现好多人遇到这个问题,我今天也遇到了,花了几个小时才解决,我把我的代码粘贴上来
主要就是要在实现一个代理方法
let locationManger = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManger.delegate = self
locationManger.desiredAccuracy = kCLLocationAccuracyBest
locationManger.distanceFilter = 10 // 指定最小更新的距离为10米
locationManger.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus){
switch(status){
case CLAuthorizationStatus.NotDetermined :
print("当前状态是:NotDetermined")
// 检测是否有requestAlwaysAuthorization 这个方法 如果有则执行
if(locationManger.respondsToSelector("requestAlwaysAuthorization")){
print("有requestAlwaysAuthorization方法即将执行该方法...")
locationManger.requestAlwaysAuthorization()
locationManger.requestWhenInUseAuthorization()
}else{
print("没有requestAlwaysAuthorization方法")
}
default:
break
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){
let location:CLLocation = locations[locations.count - 1]
if(location.horizontalAccuracy > 0 ){
print("当前位置经度:\(location.coordinate.latitude)")
print("当前位置纬度:\(location.coordinate.longitude)")
locationManger.stopUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager, rangingBeaconsDidFailForRegion region: CLBeaconRegion, withError error: NSError){
print("发生错误:\(error)")
}
举报