【发布时间】:2019-11-20 17:31:14
【问题描述】:
我正在尝试使用以下代码获取用户的当前位置,但它不起作用。我已将NSLocationWhenInUseUsageDescription 密钥和NSLocationAlwaysUsageDescription 密钥添加到我的Info.plist 文件中。
下面是代码
var locationManager = CLLocationManager();
override func viewDidLoad() {
super.viewDidLoad()
startReceivingLocationChanges();
}
func startReceivingLocationChanges() {
let authorizationStatus = CLLocationManager.authorizationStatus()
if authorizationStatus != .authorizedAlways {
// User has not authorized access to location information.
print("not authorized");
return
}
if !CLLocationManager.locationServicesEnabled() {
print("not enabled");
return
}
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
locationManager.distanceFilter = 100.0 // In meters.
locationManager.startUpdatingLocation()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let lastLocation = locations.last!
print(lastLocation)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error);
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
print("inside didChangeAuthorization ");
}
我已经阅读了苹果文档,上面是苹果建议的代码。我在这里想念什么?任何形式的帮助都非常感谢。谢谢
【问题讨论】:
-
您是否成功获取了该位置?我也有问题
-
@SUMITNIHALANI 在下面看到我的回答。
标签: swift xcode macos cllocationmanager