I've also had this question and after contacting Code Level Support got the answer.
Reference frame should be used to get magnetometer data. For example:
self.motionManager.startDeviceMotionUpdates(using: CoreMotion.CMAttitudeReferenceFrame.xMagneticNorthZVertical, to: .main) { (deviceMotionData, error) in
guard error == nil else {
print(error!)
return
}
if let motionData = deviceMotionData {
let x = motionData.magneticField.field.x
let y = motionData.magneticField.field.y
let z = motionData.magneticField.field.z
let h = motionData.heading
print(x, y, z)
print(h )
print(motionData.magneticField.accuracy.rawValue )
}
}