gpt4 book ai didi

ios - 尽管将内容添加到 plist 中,但在使用时请求授权不起作用

转载 作者:行者123 更新时间:2023-11-29 00:54:51 25 4
gpt4 key购买 nike

我构建了一个应用程序,当用户离开区域 CLCircularRegion 时发送用户的位置。我正在使用 requestWhenInUseauthorization 方法,但不会弹出询问用户是否可以使用位置的提示,但位置会更新。我的位置管理器在继承到 NSObject 的自定义类中声明..我在 View Controller 类中调用它..

这是我的代码:

class LocationProcessHandler: NSObject, CLLocationManagerDelegate {
static let sharedInstance = LocationProcessHandler()
var locationManager = CLLocationManager()

func startLocationUpdates() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters


if #available(iOS 8.0, *) {
// if self.locationManager.respondsToSelector(#selector(locationManager.requestWhenInUseAuthorization)) {
self.locationManager.requestWhenInUseAuthorization()
NSLog("The request when in use authorisation is selected")
self.locationManager.requestAlwaysAuthorization()
}
else {
self.locationManager.startUpdatingLocation()
}


NSLog("Started to monitor the significant location Changes")
self.locationManager.stopMonitoringSignificantLocationChanges()
self.locationManager.startMonitoringSignificantLocationChanges()
// NSLog("the distance filter of the location manager is \(locationManager.distanceFilter)")
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {


NSLog("Location Updated")
let persist = Persistence()

let currentLocation : CLLocation = locations[0]
let latitude : Double = currentLocation.coordinate.latitude
let Longitude : Double = currentLocation.coordinate.longitude
let regionID = "GeoFenceTrack"
let region : CLCircularRegion = CLCircularRegion.init(center: CLLocationCoordinate2DMake(latitude, Longitude), radius: Double(persist.getObject(mdmiosagent_Constants.LOCATIONRADIUS))!, identifier: regionID)

NSLog("the center of the region is \(region.center) and the redius of the region is \(region.radius)")
self.sendLocation(currentLocation)
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.delegate = self

self.locationManager.startMonitoringForRegion(region)

}

didUpdate 方法被调用,但是当应用程序启动时,或者当我禁用位置服务时,我看不到任何提示。但是这会打印在日志中

 NSLog("The request when in use authorisation is selected") 

我无法找出确切的原因。

请帮助。提前致谢。

最佳答案

在你调用requestWhenInUseAuthorization的部分,你可以这样使用:

func startLocationUpdates(){
....
//In my case my target is 8+, for this I don't check to prior version
if CLLocationManager.authorizationStatus() == .NotDetermined{
locationManager.requestWhenInUseAuthorization()
}else{
locationManager(locationManager, didChangeAuthorizationStatus: CLLocationManager.authorizationStatus())
}
....
}

然后你需要实现CLLocationManagerDelegate的委托(delegate):

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

if status == CLAuthorizationStatus.AuthorizedWhenInUse{
//Here you have the permission
self.locationManager.startUpdatingLocation()
}else if status == CLAuthorizationStatus.NotDetermined || status == CLAuthorizationStatus.Denied || status == CLAuthorizationStatus.Restricted{
//Here you can show an alert to the user (ex. to change the settings)
let alertController = UIAlertController(title: NSLocalizedString("Location Disable", comment: ""), message: NSLocalizedString("In order to use this feature you must enable your location services.", comment: ""), preferredStyle: .Alert)

let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .Cancel, handler: nil)
alertController.addAction(cancelAction)


let openAction = UIAlertAction(title: NSLocalizedString("Open Settings", comment: ""), style: .Default) { (action) in
if let url = NSURL(string:UIApplicationOpenSettingsURLString) {
UIApplication.sharedApplication().openURL(url)
}
}
alertController.addAction(openAction)

self.presentViewController(alertController, animated: true, completion: nil)
}

}

您是否还向 Info.plist 添加了 NSLocationWhenInUseUsageDescription?

关于ios - 尽管将内容添加到 plist 中,但在使用时请求授权不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37703564/

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