【问题标题】:CLLocationManager authorization issue iOS 8CLLocationManager 授权问题 iOS 8
【发布时间】:2014-08-31 10:35:31
【问题描述】:

我正在为 iOS 8 编写一段 Swift 代码。我正在尝试做一些涉及位置的事情,因此我在我的 swift 视图控制器文件中实现了以下内容:

let locationManger:CLLocationManager = CLLocationManager()

var speedReceived:Double = 0

override func viewDidLoad() {
    super.viewDidLoad()
    locationManger.delegate = self
    locationManger.desiredAccuracy = kCLLocationAccuracyBest
    let authstate = CLLocationManager.authorizationStatus()
    if(authstate == CLAuthorizationStatus.NotDetermined){
       println("Not Authorised")  
      locationManger.requestWhenInUseAuthorization()
    }
    // Do any additional setup after loading the view, typically from a nib.
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
    var location:CLLocation = locations[locations.count - 1] as CLLocation
    if(location.horizontalAccuracy > 0){
        self.speedReceived = location.speed
        println(self.speedReceived)
    }
}

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
   println("Couldn't get your location")
}

但是,我似乎无法让这段代码工作。它不会保存我对位置使用的偏好。它甚至没有提示我授予访问位置的许可。我已经尝试更新我的 info.plist。但它不起作用。顺便说一句,如果我总是在模拟器的隐私设置中选择,如果我立即切换回应用程序,它就会起作用。谁能帮忙?我确信这是问题所在,因为我在控制台上未获得授权。

有什么帮助吗?

【问题讨论】:

  • 您说您已经更新了 info.plist - 您是否仔细检查了您的条目? “NSLocationWhenInUseUsageDescription”需要与描述您的位置使用情况的字符串一起出现。一个简单的错字将阻止您的应用提示位置访问。此外,您应该始终致电locationManager.requestWhenInUseAuthorization(),而不仅仅是在您的身份未经授权时

标签: ios swift core-location cllocationmanager


【解决方案1】:

这是一个与 iOS 8 相关的问题。您必须将 NSLocationAlwaysUsageDescriptionNSLocationWhenInUseUsageDescription 键放入您的 .plist 文件中(值可能是将在位置警报中显示的附加消息)。这些键在 iOS 8 中是必需的。

Apple guidelines是怎么说的:

使用 requestAlwaysAuthorization 时需要此密钥 CLLocationManager 类的方法来请求授权 位置服务。如果此键不存在并且您调用 requestAlwaysAuthorization 方法,系统会忽略您的请求并 阻止您的应用使用位置服务。

【讨论】:

    【解决方案2】:

    我曾遇到过类似的问题,即使在将 NSLocationAlwaysUsageDescription/NSLocationWhenInUseUsageDescription 键添加到 plist 后,该问题仍然存在。

    最后,我在 plist 中添加了“隐私 - 位置使用说明”键(除了新键),瞧,它成功了!在它工作后,我能够从 plist 中删除“隐私 - 位置使用说明”键并继续成功请求授权。

    【讨论】:

      【解决方案3】:

      iOS 8 更改了位置授权策略。向后兼容的解决方案:

      SEL requestSelector = NSSelectorFromString(@"requestWhenInUseAuthorization");
      if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined &&
          [self.locationManager respondsToSelector:requestSelector]) {
          [self.locationManager performSelector:requestSelector withObject:NULL];
      } else {
          [self.locationManager startUpdatingLocation];
      }
      

      提醒:在 Info.plist 中设置 NSLocationWhenInUseUsageDescription 键

      【讨论】:

        【解决方案4】:

        我遇到了完全相同的问题。

        郑重声明,这不是官方答案。第一个答案是正确的。我只是想添加一个 FOSS (Objective-C) 项目的链接来说明修复。

        如前所述,我必须添加密钥。我的应用不需要在后台运行,所以我将NSLocationWhenInUseUsageDescription 键添加到我的 info.plist。

        如果您添加一个字符串作为此键的值(可选 - 键的存在足以设置栏),那么该字符串将出现在授权弹出窗口中。

        然后,我在所有 [CLLocationManager startUpdating] 调用之前添加了以下代码:

        if ( [locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] )
        {
            [locationManager requestWhenInUseAuthorization];
        }
        

        respondsToSelector 很重要,因为该调用仅在 iOS 8 中可用。

        第一次调用时,会显示警报。之后,它就失败了。

        注意我打电话给requestWhenInUseAuthorization

        它必须与我在 plist 中输入的值相匹配。我想你可以把两者都放,但我不知道。我不需要。

        项目是here。大部分工作(不多)都在BMLTAppDelegate.m 文件中。

        这是一个令人讨厌的惊喜。很多人不知道他们的应用程序将停止在 iOS 8 中运行。他们会做我最初做的同样的事情:在模拟器中快速运行它,注意挂起,并将其归为 beta 错误。

        现在,我有一个不同的问题:我的所有应用程序都已修复,但是当我尝试将应用程序上传到 App Store 时 Xcode 崩溃。我打开了雷达。

        Xcode 6 有点古怪。我希望补丁会很快发布。

        【讨论】:

          【解决方案5】:

          对于 Swift 2,我已将 respondsToSelector() 检查替换为 iOS 版本检查。没有那么优雅,但 Xcode 7 需要给出 0 个错误,0 个警告

          if #available(iOS 8.0, *) {
              locationManager.requestWhenInUseAuthorization()
          } else {
              locationManager.startUpdatingLocation()
          }
          

          【讨论】:

            【解决方案6】:

            由于我不喜欢直接编辑 plist,所以我总是使用 UI 来授予自动化。

            荷兰语文本“Toegang is nodig”和“Toegang is noodzakelijk”显示在使用者授予访问权限的弹出窗口中。您可以将这些更改为您喜欢的任何文本。

            只需添加到 plist 源,

            <key>NSLocationAlwaysUsageDescription</key>
            <string>To get location</string>
            <key>NSLocationWhenInUseUsageDescription</key>
            <string>To get location</string>
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-12-14
              • 1970-01-01
              • 1970-01-01
              • 2018-08-13
              • 1970-01-01
              • 2013-07-19
              相关资源
              最近更新 更多