ios - 设备磁航向代码在 iOS 8 上不起作用

标签 ios swift heading

我有以下快速代码:它的目的是找到设备当前的磁航向(即 N、E、S、W)

class ViewController: UIViewController ,CLLocationManagerDelegate {

    var lm:CLLocationManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        lm = CLLocationManager()
        lm.delegate = self

        lm.startUpdatingHeading()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {
        println(newHeading.magneticHeading)
    }
}

看起来应该有效,但事实并非如此。有人可以改进吗?

最佳答案

首先,您必须将以下行添加到您的 info.plist 中:

或者

<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

<key> NSLocationAlwaysUsageDescription</key>
<string></string>

取决于您对应用程序的真正需求。

这两个键都有描述 here .

然后,您应该请求用户授权,正如 sbarow 已经在您的问题的评论中指出的那样,通过

lm.requestWhenInUseAuthorization()

关于ios - 设备磁航向代码在 iOS 8 上不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30706697/

相关文章:

javascript - 创建一个 DIV 并给它一个 data-cart-info 属性。在 DIV 中,创建一个带有 mdc-typography--headline4"的标题作为其 CSS 类

Swift - 在另一个函数中使用 UISlider IBAction 的值

ios - swift 1.2 中的 JSON 解析问题

xcode - 准备 Segue 后 UiTextField 没有变化

html - 获得厚 h1

jupyter-notebook - Google Colab 中的编号标题和目录

ios - 发布注册应用程序的网址

ios - 如何设置可能有或没有文字的标签?

iOS Swift 使用 NO_WRAP 选项将 HEX 字符串转换为 Base64

ios - LongPressHold 开关未评估为已结束/失败(跌落和跌落卡住)