CoreLocation授权问题 - 从未提示授权

时间:2014-11-21 21:32:23

标签: ios core-location

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

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

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

我不明白为什么我必须继续将授权更改为" Always"一遍又一遍。我已经在模拟器上删除了应用程序,完成了#34; clean"从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...

1 个答案:

答案 0 :(得分:0)

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

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

NSLocationAlwaysUsageDescription
NSLocationWhenInUseUsageDescription

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

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

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