2017-04-04 92 views
1

我有requestAlwaysAuthorization我需要每次跟踪用户,如果用户不接受requestAlwaysAuthorization我想在应用程序中退出吗?CLLocation Manager check 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。基于


意见:我考虑戒烟的应用,而给用户一个可以理解的反馈非常糟糕UX。

+1

旁注:如果您的应用程序只是“退出”,而不是显示的一个错误消息,它会让苹果拒绝,如果从App Store。更重要的是,阻止用户访问你的应用程序,如果他们不给你一些权限,也是一个拒绝原因。 –

1

以上回答也很好,我只是试图用方法使它变得简单。此外,如果您正在使用硬件设备(如信标),则必须访问授权总是

检查位置服务启用

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) 
}