gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:07:28 26 4
gpt4 key购买 nike

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

 @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
}

}

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

最佳答案

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

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

        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 委托(delegate)尚未触发),我会运行我的函数来提示转到应用程序设置。

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

关于ios - 在设置中禁用位置后无法重新启用位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32184916/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com