核心位置在模拟器中不工作 Xcode 9.4.1
Core Location not working in simulator Xcode 9.4.1
我有以下代码:
import CoreLocation
class ViewController: UIViewController, NetworkingDelegate, CLLocationManagerDelegate {
let locationManager: CLLocationManager = CLLocationManager()
之后:
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
然后我在 VC
中实现委托方法
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
for currentLocation in locations{
print("\(index)\(currentLocation)")
}
print("Did Location Delegate get called?")
let location: CLLocation = locations.first as! CLLocation
print(location)
self.lat = String(format: "%f", location.coordinate.latitude)
self.long = String(format: "%f", location.coordinate.longitude)
}
在构建阶段,"Link Binary With Libraries" 已选择 CoreLocation.framework。
存在 plist 中的适当隐私:隐私 - 位置使用说明以及隐私 - 使用时的位置使用说明。
仍未获取位置。我在 Xcode 9.4.1 中使用模拟器,目标是 iOS 11。我在这里缺少什么?
问题是 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
已经过时多年了。更新现代 Swift 的代码。声明应如下所示:
我有以下代码:
import CoreLocation
class ViewController: UIViewController, NetworkingDelegate, CLLocationManagerDelegate {
let locationManager: CLLocationManager = CLLocationManager()
之后:
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
然后我在 VC
中实现委托方法func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
for currentLocation in locations{
print("\(index)\(currentLocation)")
}
print("Did Location Delegate get called?")
let location: CLLocation = locations.first as! CLLocation
print(location)
self.lat = String(format: "%f", location.coordinate.latitude)
self.long = String(format: "%f", location.coordinate.longitude)
}
在构建阶段,"Link Binary With Libraries" 已选择 CoreLocation.framework。
存在 plist 中的适当隐私:隐私 - 位置使用说明以及隐私 - 使用时的位置使用说明。
仍未获取位置。我在 Xcode 9.4.1 中使用模拟器,目标是 iOS 11。我在这里缺少什么?
问题是 func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
已经过时多年了。更新现代 Swift 的代码。声明应如下所示: