title: How to get magnetometer data using swift?
body: <p>I am trying to get the magnetic field data from my iphone 6 by using CoreMotion.</p>
  
  <p>I had no problems accessing the raw data with the following code: </p>
  
  <pre><code>if available {
          motionMangager.magnetometerUpdateInterval = updateInterval
          motionMangager.startMagnetometerUpdatesToQueue(queue, withHandler: {
              (data, error: NSError!) -&gt; Void in
              println("x: \(data.magneticField.x), y: \(data.magneticField.y), z: \(data.magneticField.z)")
          })
      }
  </code></pre>
  
  <p><strong>BUT: I need the derived data by using an device motion instance.</strong> </p>
  
  <p>So I did the following: </p>
  
  <pre><code>if motionMangager.deviceMotionAvailable {
          motionMangager.magnetometerUpdateInterval = updateInterval
          motionMangager.startDeviceMotionUpdatesUsingReferenceFrame(CMAttitudeReferenceFrameXArbitraryZVertical, toQueue: queue, withHandler: {
              (deviceMotion: CMDeviceMotion!, error: NSError!) -&gt; Void in
              // If no device-motion data is available, the value of this property is nil.
              if let motion = deviceMotion {
                  println(motion)
                  var accuracy = motion.magneticField.accuracy
                  var x = motion.magneticField.field.x
                  var y = motion.magneticField.field.y
                  var z = motion.magneticField.field.z
                  println("accuracy: \(accuracy.value), x: \(x), y: \(y), z: \(z)")
              }
              else {
                  println("Device motion is nil.")
              }
          })
      }
  </code></pre>
  
  <p><strong>And here is the problem:</strong> </p>
  
  <p>I am always getting zero for the field coordinates x, y and z. The accuracy as well is -1. According to the Apple Documentation an accuracy of -1 means "CMMagneticFieldCalibrationAccuracyUncalibrated" means "the device does not have a magnetometer"... But no! It is an iPhone 6... </p>
  
  <p>So what am I doing wrong? I tried all four CMAttitudeReferenceFrame. Please I need help. Any ideas?</p>