CLLocation Manager 检查 requestAlwaysAuthorization 如果不接受退出应用
Posted
技术标签:
【中文标题】CLLocation Manager 检查 requestAlwaysAuthorization 如果不接受退出应用【英文标题】:CLLocation Manager check requestAlwaysAuthorization and if not accept exit app 【发布时间】:2017-08-29 06:43:08 【问题描述】:我有requestAlwaysAuthorization
,如果用户不接受requestAlwaysAuthorization
,我每次都需要跟踪用户我想退出应用程序吗?
我该怎么做?
我的代码在下面。
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)")
【问题讨论】:
【参考方案1】:在错误情况下,此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。
基于意见:我考虑退出应用程序而不是给用户一个可以理解的反馈非常糟糕的用户体验。
【讨论】:
旁注:如果您的应用只是“退出”而不是显示错误消息,如果来自 App Store,它将使 Apple 拒绝。更重要的是,如果用户不授予您某些权限,则阻止他们访问您的应用也是一种拒绝原因。【参考方案2】:上面的答案也很好,我只是试着用方法让它变得简单一些。此外,如果您使用信标等硬件设备,则必须访问位置 AuthorizedAlways。
检查定位服务是否启用
public func isLocationEnabled()-> Bool
if CLLocationManager.locationServicesEnabled()
switch(CLLocationManager.authorizationStatus())
case .NotDetermined, .Restricted, .Denied , .AuthorizedWhenInUse :
showLocationServiceNotEnabledAlert()
return false
case .AuthorizedAlways: // As of now we check for only "Always", not for "When In Use" this should be fixed according to the requirements
return true
return false
提醒用户使用服务并重定向到设置
func showLocationServiceNotEnabledAlert()
let title = "Your Title"
let message = "Your Message"
let alertController = UIAlertController(title: title, message: message, preferredStyle: .Alert)
let settingsAction = UIAlertAction(title: "Settings".localized, style: .Default) (alertAction) in
if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString)
UIApplication.sharedApplication().openURL(appSettings)
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel".localized, style: .Cancel, handler: nil)
alertController.addAction(cancelAction)
UIApplication.sharedApplication().delegate?.window!?.currentViewController?.presentViewController(alertController, animated: true, completion: nil)
【讨论】:
以上是关于CLLocation Manager 检查 requestAlwaysAuthorization 如果不接受退出应用的主要内容,如果未能解决你的问题,请参考以下文章
未调用 CLLocation Manager didStartMonitoringForRegion 委托方法
CLLocation Manager 有时会给出不正确的位置
CLLocation Manager 使用默认位置而不是使用实际位置进行更新
iOS - CLLocation Manager didUpdateLocations 调用了太多时间
iOS - CLLocation Manager didUpdateLocations 在一个类中被调用,而不是在另一个类中?