在设置中禁用位置后无法重新启用位置

时间:2015-08-24 14:32:39

标签: ios swift cllocationmanager

在我的应用中,我请求访问位置服务的权限。这完美地使用

 @IBAction func enableLocation(sender: AnyObject) {

    let status = CLLocationManager.authorizationStatus()

    if status != .AuthorizedAlways{

        print("button pressed \(status)")

        self.manager.delegate = self
        self.manager.requestAlwaysAuthorization()
        self.manager.requestWhenInUseAuthorization()

    } else {

        performSegueWithIdentifier("locationEnabled", sender: nil)
    }

}


 func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {

        print("Auth Changed")

        switch status {
        case .NotDetermined:
            self.manager.requestAlwaysAuthorization()
            break
        case .AuthorizedWhenInUse:

            self.activityIndicator.hidden = false
            self.activityIndicator.isAnimating()

            performSegueWithIdentifier("locationEnabled", sender: nil)

            break
        case .AuthorizedAlways:

            self.activityIndicator.hidden = false
            self.activityIndicator.isAnimating()

            performSegueWithIdentifier("locationEnabled", sender: nil)

            break
        case .Restricted:
            // restricted by e.g. parental controls. User can't enable Location Services

            break
        case .Denied:


            // user denied your app access to Location Services, but can grant access from Settings.app
            break
        default:
            break
        }

    }

但是,当我在设置中专门为应用程序禁用位置时,按下按钮时拒绝重新启用它。如果用户取消激活设置中的位置,是否可以在应用中重新启用?

1 个答案:

答案 0 :(得分:0)

我的解决方案可能不是最漂亮的,但它解决了我的问题

首先,如果位置已被接受,您需要检查标准。在该检查中,我延迟了一个方法,以查看是否通过简单的BOOL触发了locationManager委托。

        let delay = 0.5 * Double(NSEC_PER_SEC)
        let time = dispatch_time(DISPATCH_TIME_NOW, Int64(delay))
        dispatch_after(time, dispatch_get_main_queue(),{

                if(!self.locationApproved){
                    self.openSettings()
                }
        })

我在locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus)

中加了一个BOOL
locationApproved = true

如果该位置尚未被批准(即locationManger委托没有被解雇),我运行我的功能以提示进入应用程序设置。

func openSettings(){

    let uiAlert = UIAlertController(title: "Location not authorised", message: "Looks like you have turned off your location. We need this to show you the closest offers", preferredStyle: UIAlertControllerStyle.Alert)
    self.presentViewController(uiAlert, animated: true, completion: nil)

    uiAlert.addAction(UIAlertAction(title: "Go to settings", style: .Default, handler: { action in
        let settingsUrl = NSURL(string: UIApplicationOpenSettingsURLString)
        if let url = settingsUrl {
            UIApplication.sharedApplication().openURL(url)
        }
    }))

    uiAlert.addAction(UIAlertAction(title: "No", style: .Cancel, handler: { action in
        print("Click of cancel button")
    }))