我编写了一个使用设备位置的基本程序。我写了它,这是ViewController的基础知识
mHandler.removeCallbacks(job);
mHandler.postDelayed(job, 3000);
在我的info.plist中,根据需要,我有这个:
override func viewDidLoad() {
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("Location Updated")
lat = String(format: "%f", locationManager.location!.coordinate.latitude)
long = String(format: "%f", locationManager.location!.coordinate.longitude)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Failed Loc: \(error)")
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("Auth Changed: \(status)")
}
我在模拟器上运行代码,它给了我
<key>NSLocationWhenInUseUsageDescription</key>
<string>uses loc</string>
然后我分离了模拟器的位置并打印
Failed Loc: Error Domain=kCLErrorDomain Code=0 "(null)"
应该如此。奇怪;但是,当我将其上传到我的ios设备上时,它会要求许可,然后我没有输出。输出没有错误,它甚至不会触发didChangeAuthorizationStatus
**编辑:这是ViewController文件的标题:
Location Updated
**编辑2:
import CoreLocation
class Central_ViewController: UIViewController, UIScrollViewDelegate, UITabBarDelegate, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
打印
self.locationManager.requestWhenInUseAuthorization()
if(CLLocationManager.locationServicesEnabled()) {
print("Location Services Enabled")
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
}else {
print("Location Services NOT Enabled") // This needs to be completed to produce an error
}
但它仍然不会更新位置,也不会产生错误。
答案 0 :(得分:0)
首先,您应该检查NSLocationWhenInUseUsageDescription
是否在相应的Info.plist中,即应用的那个。这是因为项目中有很多Info.plists,甚至GoogleMaps框架都有一个。
一旦正确的Info.plist包含NSLocationWhenInUseUsageDescription
,您会注意到应用会提示您允许获取位置。
然后不要使用locationServicesEnabled
来检查是否允许使用位置服务。请改用以下内容,以检查授权:
if CLLocationManager.authorizationStatus == .NotDetermined {
self.locationManager.requestWhenInUseAuthorization()
}
self.locationManager.startUpdatingLocation()