ios - CLLocationManager 授权问题 iOS 8

标签 ios swift core-location cllocationmanager

我正在为 iOS 8 编写一段 Swift 代码。我正在尝试做一些涉及位置的事情,所以我在我的 swift View Controller 文件中实现了以下内容:

让 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。但它不起作用。顺便说一句,如果我在模拟器的隐私设置中选择始终,如果我立即切换回应用程序,它就会起作用。谁能帮忙?我确定这就是问题所在,因为我在我的控制台上未获得授权。

有什么帮助吗?

最佳答案

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

Apple guidelines 中是怎么说的? :

This key is required when you use the requestAlwaysAuthorization method of the CLLocationManager class to request authorization for location services. If this key is not present and you call the requestAlwaysAuthorization method, the system ignores your request and prevents your app from using location services.

关于ios - CLLocationManager 授权问题 iOS 8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24669985/

相关文章:

ios - 在tableview中插入和删除单元格,将文本字段放入单元格中,然后将文本字段数据放入swift中的json原始参数中

iphone - 如何从 iPhone 核心位置获取当前(美国)状态?

ios - 使用动画快速更改圆形边框颜色

ios - 如何在单独的线程中运行整个对象(DAO.h 和 .m)

ios - 防止 Xcode 在为 iOS 运行时准备 Apple Watch

ios - 向 UIView 添加圆角会禁用 touchesBegan

ios - 未调用 BLE 外设委托(delegate)

ios - UIKit:是否可以为UIKeyboard创建一个类别?

ios - 向框架请求位置许可?

iOS swift : Request user location