首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CoreLocation授权问题-从未提示授权

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

Stack Overflow用户
提问于 2014-11-21 21:32:22
回答 1查看 54关注 0票数 1

我正在测试CoreLocation,以学习如何捕获和记录用户的位置。我建立了一个简单的主细节应用程序,在细节窗格中,显示用户的实时位置。一切都如期而至。

接下来,我继续制作我的真正的应用程序,它也使用CoreLocation。我建立了一个主细节风格的应用程序,并使它也使它当用户打开详细窗格,它应该显示他们的当前,现场,位置。然而,什么也没有发生。

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

我不明白为什么我必须一遍又一遍地将授权更改为“始终”。我已经删除了模拟器上的应用程序,从XCode上做了一个“干净”来重新开始,但是它还是未经授权在模拟器上启动。

有人有主意吗?

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

下面是我的代码(它在另一个应用程序上工作):

代码语言:javascript
复制
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...
EN

回答 1

Stack Overflow用户

发布于 2015-12-17 22:21:16

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

请确保在plist中有两个键作为string类型,并且具有要向用户显示的值:

NSLocationAlwaysUsageDescription

NSLocationWhenInUseUsageDescription

代码语言:javascript
复制
- (void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{

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

        if ([CLLocationManager locationServicesEnabled]) {
            [yourLocationManager startMonitoringSignificantLocationChanges];
            //run your code here
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27070729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档