gpt4 book ai didi

ios - CLLocation Manager 检查 requestAlwaysAuthorization,如果不接受则退出应用程序

转载 作者:搜寻专家 更新时间:2023-11-01 07:14:09 26 4
gpt4 key购买 nike

我有 requestAlwaysAuthorization,如果用户不接受 requestAlwaysAuthorization 我想在应用程序中退出?

How can I do it ?

我的代码在下面。

import CoreLocation

public var locationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()


locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()

}



func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

let altitudeG = locations.last?.altitude
let longitudeG = locations.last?.coordinate.longitude
let latitudeG = locations.last?.coordinate.latitude

print("\(altitudeG) \(longitudeG) \(latitudeG)")

}

最佳答案

在错误情况下调用此委托(delegate)方法:

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
// handle not authorized error here. You might want to quit on specific errors (unauthorized) only. Check the error.

UIControl().sendAction(#selector(URLSessionTask.suspend), to: UIApplication.shared, for: nil)
}

您还可以在让 CLLocationManager 失败之前检查当前权限状态:

if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
print("Access")
}
} else {
print("Location services are not enabled")
}

拍摄 from this answer .


基于意见:我考虑退出该应用程序,而不是向用户提供可理解的非常糟糕的用户体验反馈。

关于ios - CLLocation Manager 检查 requestAlwaysAuthorization,如果不接受则退出应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43200898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com