【发布时间】:2019-02-18 09:02:09
【问题描述】:
我正在尝试获取用户的纬度和经度值,但没有调用 CLLocationManagerDelegate 的方法。提前感谢任何可以帮助我解决此问题的人。
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationmanager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationmanager.delegate = self;
locationmanager.desiredAccuracy = kCLLocationAccuracyBest
locationmanager.requestAlwaysAuthorization()
locationmanager.startUpdatingLocation()
}
//MARK: - location delegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation :CLLocation = locations[0] as CLLocation
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
}
【问题讨论】:
-
您最初是否收到警报提示
-
你在 info.plist 中添加了
NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription吗? -
并在 iPhone 设置中检查您的应用程序的授予访问权限。您的应用程序应该可以访问位置服务。
-
我添加了 NSLocationAlwaysUsageDescription,我也收到了该警报。但我没有获得纬度和经度值,我正在模拟器中运行我的应用程序
-
请在设备代码上测试它是否完美。并确保 iPhone 设备位置隐私将打开。对于模拟器测试,请按照此步骤转到顶部栏并选择调试 -> 位置 -> 苹果。然后运行你的项目,它会工作。
标签: ios swift4 core-location