【问题标题】:How to know if user rejected the Location Services in Swift 2如何知道用户是否拒绝了 Swift 2 中的定位服务
【发布时间】:2016-05-16 11:54:59
【问题描述】:

我正在尝试制作定位服务应用程序,并且我有以下代码,因此当用户转到该视图控制器时,他会收到获取当前位置的警报。

这是代码

override func viewDidLoad() {
        super.viewDidLoad()
// 1. status is not determined
        if CLLocationManager.authorizationStatus() == .NotDetermined {
            locationManager.requestAlwaysAuthorization()
        }
            // 2. authorization were denied
        else if CLLocationManager.authorizationStatus() == .Denied {



            SwiftSpinner.hide()
            let alert = UIAlertController(title: "Error with Your Location" , message: "Location services were previously denied. Please enable location services for this app in Settings.", preferredStyle: UIAlertControllerStyle.Alert)
            let ok = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) {
                UIAlertAction in
                UIApplication.sharedApplication().openURL(NSURL(string: UIApplicationOpenSettingsURLString)!)


            }
            alert.addAction(ok)
            let cancel = UIAlertAction(title: "Back", style: UIAlertActionStyle.Default) {
                UIAlertAction in

                self.movenav("arxiki")

            }
            alert.addAction(cancel)
            self.presentViewController(alert, animated: true, completion: nil)

        }
            // 3. we do have authorization
        else if CLLocationManager.authorizationStatus() == .AuthorizedAlways {
            locationManager.startUpdatingLocation()
        }


        self.navigationController?.setNavigationBarHidden(false, animated: true)

       self.eventsTable.backgroundColor = UIColor.lightGrayColor()

       // self.locationManager.requestAlwaysAuthorization()

        if CLLocationManager.locationServicesEnabled() {
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
            locationManager.startUpdatingLocation()
        } 

    }

我的问题如下。 如果用户推送“不授权”,我如何获得他的选项,以便我可以将他发送回之前的视图控制器或用我拥有的消息提醒他?

【问题讨论】:

    标签: swift2 cllocationmanager


    【解决方案1】:

    为了捕捉用户选择,您需要声明一个 CLLocationManager 对象并实现其委托 (CLLocationManagerDelegate),并使用以下方法来捕捉它。

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        if status == .Denied || status == .NotDetermined{
           // User selected Not authorized
        }       
    }
    

    我假设您已经使用合适的位置参数配置了 info.plist。

    希望对你有帮助!

    【讨论】:

    • 谢谢。这确实有帮助。 Swift 5 中的命令已更改为:func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 2023-04-11
    • 2012-01-29
    • 2019-05-29
    • 1970-01-01
    相关资源
    最近更新 更多