显示位置权限对话框并立即消失
Location permission dialog is shown and immediately disappears
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,模拟器和真实设备
您的 locationManager
变量不会超出其定义范围(该代码片段所在的函数),因此在用户响应对话框之前将其释放。
如果您将 let locationManager = CLLocationManager()
移动到 class 变量,它应该会保留下来。
将 let locationManager = CLLocationManager()
插入 ViewController
class 的开头。 (在 viewDidLoad()
函数之外)。希望对你有帮助。
class ViewController: UIViewController {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
}
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,模拟器和真实设备
您的 locationManager
变量不会超出其定义范围(该代码片段所在的函数),因此在用户响应对话框之前将其释放。
如果您将 let locationManager = CLLocationManager()
移动到 class 变量,它应该会保留下来。
将 let locationManager = CLLocationManager()
插入 ViewController
class 的开头。 (在 viewDidLoad()
函数之外)。希望对你有帮助。
class ViewController: UIViewController {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
}
}