设备磁标题代码无法运行iOS 8

时间:2015-06-08 10:25:40

标签: 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)
    }
}

它似乎应该有效,但事实并非如此。任何人都可以改进吗?

1 个答案:

答案 0 :(得分:0)

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

要么

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

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

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

两个键都被描述为here

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

lm.requestWhenInUseAuthorization()