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

时间:2017-04-04 07:30:18

标签: ios swift swift3 cllocationmanager

我有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)")

    }

2 个答案:

答案 0 :(得分: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

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

答案 1 :(得分:1)

上面的答案也很好,我只是试着用方法轻松一点。此外,如果您正在使用信标等硬件设备,则必须访问 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)
}