【问题标题】:Code for device magnetic heading not working iOS 8设备磁航向代码不起作用 iOS 8
【发布时间】:2015-06-08 10:25:40
【问题描述】:

我有以下 swift 代码:用于查找设备的当前磁航向(即 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)
    }
}

它似乎应该工作,但它没有。有人可以改进吗?

【问题讨论】:

  • 您需要先询问用户是否可以使用位置服务。 requestWhenInUseAuthorization()

标签: ios swift heading


【解决方案1】:

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

要么

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

或

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

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

两个键都被描述为here。

然后,您应该通过

请求用户授权,正如 sbarow 在 cmets 中对您的问题所指出的那样
lm.requestWhenInUseAuthorization()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-22
    • 2019-06-26
    • 2018-09-20
    相关资源
    最近更新 更多