我想使用位置数据制作一个简单的iOS应用程序。不幸的是,即使在模拟器上输入“当前位置”调试代码,在设备和模拟器上同时测试时,我也会收到'无'。 这是我第一次在Swift 3上使用CoreLocation,所以我使用了和前面一样的方法。位置快速问题3
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var latitudeLabel: UILabel!
@IBOutlet weak var longitudeLabel: UILabel!
var locationManager:CLLocationManager?
var currentLocation:CLLocation?
override func viewDidLoad() {
super.viewDidLoad()
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
updateLabels()
// Do any additional setup after loading the view, typically from a nib.
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
self.currentLocation = locations[0]
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print(error)
}
func updateLabels() {
latitudeLabel.text = String(describing: currentLocation?.coordinate.latitude)
longitudeLabel.text = String(describing: currentLocation?.coordinate.longitude)
print(currentLocation)
}
}
当然,我已经在Info.plist中编写了所有必要的隐私密钥。
当我试图打印currentLocation时,我收到零。 随着最后一次发布,我发现这样的问题,而不是一个警报正在出现,但立即消失
你说的“我接受‘零’”是什么意思?请澄清你的问题。 – rmaddy
感谢您的回复。编辑说明。 –