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

CLLocation Manager check requestAlwaysAuthorization and if not accept exit app

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

已拍摄


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

上面的回答也很好,我只是想用方法让它变得简单一些。此外,如果您使用的是信标等硬件设备,那么您必须访问位置 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)
}