【问题标题】:CoreLocation Authorization issues - never prompted for authorizationCoreLocation 授权问题 - 从未提示授权
【发布时间】:2014-11-21 21:32:22
【问题描述】:

我正在测试 CoreLocation 以了解如何捕获和记录用户的位置。我构建了一个简单的 Master-Detail 应用程序,它在详细信息窗格中显示用户的实时位置。一切都按预期进行。

接下来,我开始制作我的真实应用程序,该应用程序也使用 CoreLocation。我使用 Master-Detail 样式构建了应用程序,并且还制作了它,以便当用户打开详细信息窗格时,它应该显示他们当前的实时位置。然而,什么都没有发生。

经过大量调试和研究后,我确定的是,一旦我的locationManager 被创建并调用locationManager.startUpdatingLocation() 授权get,位置就会更改为denied(或假,或任何名称)。我可以获得实时跟踪的唯一方法是在 Xcode 中构建和运行应用程序,一旦它在模拟器中打开,打开位置服务并更改应用程序设置以允许位置跟踪为“始终”。然后我可以在控制台中看到正在获取位置。

我不明白为什么我必须一遍又一遍地将授权更改为“始终”。我已经删除了模拟器上的应用程序,从 XCode 进行了“清理”以重新开始,但它仍然在未经授权的情况下在模拟器上启动。

有人有想法吗?

更新现在我看到,当我构建并在模拟器中运行时,我得到一个位置,然后授权更改为不允许位置服务。

这是我的代码(适用于另一个应用程序):

import UIKit
import CoreLocation

class DetailViewController: UIViewController, CLLocationManagerDelegate {

var locationManager : CLLocationManager!



func startTrackingLocation() {
    println("tracking engaged")
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.requestWhenInUseAuthorization() // <-- this was originally commented out
    locationManager.startUpdatingLocation()
    println("and we're tracking")
    println(locationManager.location)
}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    println("location acquired") // <--- Only see this when I manually allow location when app is running
    self.newLongLabel?.text = "\(locations[0].coordinate.longitude)"
    self.newLatLabel?.text = "\(locations[0].coordinate.latitude)"
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.startTrackingLocation()
    self.configureView()
}

// more class stuff...

【问题讨论】:

  • 不要同时要求两者,选择一个:requestAlwaysAuthorizationrequestWhenInUseAuthorization(我想你在plist中已经有了这两个键)
  • @TonyMkenu - 我删除了requestAlwaysAuthorization 并没有改变这种情况。 ://
  • 你确定,你有 plist 中的密钥吗? &lt;key&gt;NSLocationWhenInUseUsageDescription&lt;/key&gt; 还是永远?
  • @TonyMkenu Ya,当然可以。
  • 您使用的是哪个 iOS 版本?

标签: ios core-location


【解决方案1】:

如果您确定已将密钥添加到 plist 文件,请尝试在 didChangeAuthorizationStatus: 位置管理器委托方法中检查它。

确保 plist 中的两个键都作为字符串类型,并且具有您希望向用户显示的值:

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription

- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

    if (status == kCLAuthorizationStatusAuthorizedWhenInUse || status == kCLAuthorizationStatusAuthorizedAlways){

        if ([CLLocationManager locationServicesEnabled]) {
            [yourLocationManager startMonitoringSignificantLocationChanges];
            //run your code here
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 2013-07-19
    • 2013-11-01
    • 2022-08-15
    • 2018-02-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多