【问题标题】:CLLocation Manager check requestAlwaysAuthorization and if not accept exit appCLLocation Manager 检查 requestAlwaysAuthorization 如果不接受退出应用
【发布时间】: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)")

    }

【问题讨论】:

    标签: ios swift swift3 cllocationmanager


    【解决方案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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多