1
votes

I'm looking to get the exact current pitch, yaw and roll of an iPhone device, relative to a base position. A lot like what is done in the Compass App. Most of what I have found online is about detecting changes in the result, while I am looking for an absolute position relative to a base position, the base position (pitch: 0, roll: 0, yaw: 0) being this position:

Can anybody suggest what I should do to get this? I need this one-off, basically as a result from function call.

1

1 Answers

2
votes

The device does not track the orientation constantly. Your app needs to indicate it needs this data (through the Info.plist file) and start monitoring it before you can access it.

The information is accessible though the deviceMotion property of the CMMotionManager class. It is an optional, indicating that it is nil if the device doesn't support this or if you didn't start monitoring the data beforehand.

You need to specify a reference frame that you can use for this purpose to startDeviceMotionUpdates(using:) so that you get proper values. What you want is probably .xTrueNorthZVertical. Note that the documentation for this states that:

Note that using this reference frame may require device movement to calibrate the magnetometer. It also requires the location to be available in order to calculate the difference between magnetic and true north.

This probably means that you can't just read it spontaneously. You will probably need to start monitoring when your app starts or the user enters the view where you need it, and check the value later when the calibration is done and the user does something to need it.