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

标签 ios swift cllocationmanager

我构建了一个应用程序,当用户离开区域 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/

相关文章:

ios - 如何首先按文件夹、名称、大小、日期对 GTLQueryDrive 项目进行排序

ios - 如何在 WebKit View 中获取点击元素的 Id?

ios - 在 Swift 3.0 中动画完成后导航到新 View Controller

html - Swift:无法在 webView 中加载 HTML

ios - Swift 中的反向地理编码位置

ios - 如何使用 UIAppearance 设置 ViewController 的背景颜色

ios - 读/写NSSet/NSMutableSet到文件

swift - MD5 3DES 加密 Swift

ios - 协议(protocol)扩展不能满足 CLLocationManagerDelegate 一致性?

ios - startMonitoringForRegion 不会触发 didEnterRegion 和 didExitRegion