半秒后,iOS对话框会提示并消失:
let locationManager = CLLocationManager()
switch CLLocationManager.authorizationStatus() {
case .authorizedWhenInUse:
print("In Use \(locationManager.location?.description)")
case .denied, .restricted:
print("denied")
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
case .authorizedAlways:
print("always \(locationManager.location)")
}我不知道这是否相关,但我使用的是SWReavealViewController。Xcode9,为iOS 8.0编译,包括模拟器和真实设备
发布于 2017-10-20 01:56:48
您的locationManager变量不会超出其定义的作用域(该代码片段所在的函数),因此在用户可以响应对话框之前将其释放。
如果您将let locationManager = CLLocationManager()上移到一个类变量,它应该会一直存在。
发布于 2018-08-21 04:22:31
在您的ViewController类的最前面插入let locationManager = CLLocationManager()。(在viewDidLoad()函数之外)。希望能对你有所帮助。
class ViewController: UIViewController {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
}https://stackoverflow.com/questions/46835674
复制相似问题