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

Can't re-enable location after disabling it in settings

在我的应用程序中,我正在请求访问定位服务的权限。使用

效果很好
 @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
        }

    }

但是,当我在设置中禁用专门针对应用程序的位置时,它拒绝在按下按钮时重新启用它。如果用户在设置中停用位置,是否无法在应用程序中重新启用?

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

首先,您需要按照标准检查位置是否已被接受。在该检查中,我延迟了一个方法来查看是否已通过简单的 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")
    }))