在 Swift 中制作指南针应用程序 iOS

Making a Compass Application iOS in Swift

我正在尝试将指南针模块集成到我的应用程序中。我使用 CoreLocation 框架并将 CLLocationManagerDelegate 添加到我的 class。我正在用这段代码初始化我的 class。

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()

然后我调用了下面的函数,但我没有得到任何输出。

func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    let h = newHeading.magneticHeading
    let k = newHeading.trueHeading
    print(h)
    print(k)
    label.text = "\(h)"
}

我将 NSLocationWhenInUseUsageDescription 添加到我的 Info.plist

提前致谢。

你错过了..

locationManager.startUpdatingHeading

//Starts the generation of updates that report the user’s current heading.
- startUpdatingHeading

//Starts the generation of updates that report the user’s current location.
- startUpdatingLocation