I am very new to coding in general and have been following lots of tutorials to get where I am right now. I followed this other question of stackoverflow: Finding the compass orientation of an iPhone in swift
Following the code in each answer, nothing is being printed. This is my code:
import UIKit
import CoreLocation
class PointingViewController: UIViewController, CLLocationManagerDelegate {
var locationManager: CLLocationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print(newHeading.magneticHeading)
}
@IBAction func firstButtonPressed(_ sender: Any) {
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
}
Ideally, the compass orientation is recorded whenever the button is pressed. I have tried putting the .startUpdatingLocation()
and .startUpdatingHeading
in the viewDidLoad()
function, but no change occurs.
Is there a better way to accomplish what I desire?