KeeperOfKale
KeeperOfKale

Reputation: 721

Beginner: Need Help Accessing Magnetometer Data using Swift

TLDR - Goal: Build a simple compass app

I'm brand new at coding and I'm challenging myself to try and learn something new. I've studied up on the main components of coding and followed a few simple tutorials. I've also been learning a lot on here.

However, I'm still having a hard time understanding how to retrieve the magnetometer data. I've read the Apple documentation in Obj-C and Swift (with Swift being new, compass tutorials are almost exclusively done in Obj-C), but they are still confusing, to someone who is as new as I am, when trying to convert that over to swift.

I've looked up similar questions and even tried to run the code from them and I usually get at least 1 error saying something is expected or missing or not defined. Preventing me from even testing it... Which has led me down a long road of trying to understand Xcode's error syntax.

I know I have to test on my device, otherwise I won't see any data. I also know I have to import CoreMotion, then use CMMotionManager as sort of a 'gateway' to retrieving the data, and then define the update intervals. But I'm unclear on how to go about structuring that part of code that actually does the retrieving of the data.

In short I just want to have the barebones simplest route to retrieving the data and have it print while I move my phone. Dumbed down explanations of the process are welcome. I promise I won't be offended, in fact I'd be delighted.

Anyone in a teaching mood?

Upvotes: 0

Views: 1722

Answers (2)

julie m
julie m

Reputation: 11

You need to be sure your app has properly asked for permission and the plist is properly set up

code snippet:

    // don't forget iOS 8 wants the plist value too!
    if (locationManager.respondsToSelector(Selector("requestWhenInUseAuthorization"))) {
        locationManager.requestWhenInUseAuthorization()
    }

Upvotes: 1

Teemu Kurppa
Teemu Kurppa

Reputation: 4839

If you want to build a simple compass app, I recommend you start by using CLLocationManager instead of CMMotionManager. You implement locationManager(didUpdateHeading:) method of CLLocationManagerDelegate (docs). In addition, you create a CLLocationManager (docs) and start heading updates for example in viewDidLoad You can do it for example in your view controller, that XCode creates for you:

class MyViewController: UIViewController, CLLocationManagerDelegate {
     var locationManager: CLLocationManager!

     override func viewDidLoad() {
         super.viewDidLoad()
         locationManager = CLLocationManager()
         locationManager.delegate = self
         locationManager.startHeadingUpdates()
     }


     override func locationManager(_ manager: CLLocationManager!,
                 didUpdateHeading newHeading: CLHeading!) {
          println("New heading \(newHeading.magneticHeading)")
          // TODO: update your UIViews that draw the compass here.             
     }
}

This gets you started, but for the real application, you might want to start and stop heading updates in viewWillAppear and viewDidDisappear methods.

Upvotes: 1

Related Questions