Core Motion in iOS 8 and Xamarin

Core Motion is having an update with the introduction of iOS 8 and we get several new classes to play with.


The first of these being CMPedometer, this looks to take the place of CMStepCounter which came about with iOS 7. What do we get over and above the old step counter? Here is the new data broken down:

  • Number of steps
  • Distance travelled
  • Speed (Based on calculating from distance over time)
  • Floors ascended (Requires information about the building to be available)
  • Floors descended (Requires information about the building to be available)

The implementation of this is really simple and looks a little like this:

public override async void ViewDidLoad ()
   base.ViewDidLoad ();

   this.pedometer = new CMPedometer ();
   this.pedometer.StartPedometerUpdates (new NSDate (), this.UpdatePedometerData);

   var data  = await this.pedometer.QueryPedometerDataAsync (DateTime.SpecifyKind (DateTime.Now.AddHours(-24), DateTimeKind.Utc), DateTime.Now);
   this.UpdatePedometerData(data, null);

private void UpdatePedometerData(CMPedometerData data, NSError error)
   if (error == null) {
      this.InvokeOnMainThread (() => {
         this.StepsLabel.Text = string.Format ("You have taken {0:0.00} steps", data.NumberOfSteps);
         this.DistanceLabel.Text = string.Format ("You have travelled {0:0.00} meters", data.Distance.DoubleValue);
         var endTime = DateTime.SpecifyKind(data.EndDate, DateTimeKind.Utc);
         var startTime = DateTime.SpecifyKind(data.StartDate , DateTimeKind.Utc);
         var time = endTime.Subtract(startTime);
         var speed = data.Distance.DoubleValue / time.TotalSeconds;
         this.SpeedLabel.Text = string.Format ("Your speed was {0:0.00} meters a second", speed);
         this.FloorsAscendedLabel.Text = string.Format("You have ascended {0} floors", data.FloorsAscended);
         this.FloorsDescendedLabel.Text = string.Format("You have descended {0} floors", data.FloorsDescended);


The second class brand new to iOS 8 is CMAltimeter which seems to point to the new hardware to be made available with the next iteration of the iPhone / iPad. The main function here is to get the relative altitude of the device but this is currently not supported on any available hardware.

Here is a look at the implementation though as it stands:

public override void ViewDidLoad ()
   base.ViewDidLoad ();
   if (CMAltimeter.IsRelativeAltitudeAvailable) {
      this.altimeter = new CMAltimeter ();
      this.altimeter.StartRelativeAltitudeUpdates (NSOperationQueue.MainQueue, this.UpdateAltitudeData);
   } else {
      this.AltitudeLabel.Text = "Your device does not have required hardware for altitude..";

private void UpdateAltitudeData(CMAltitudeData data, NSError error)
   this.InvokeOnMainThread (() => {
      this.AltitudeLabel.Text = string.Format ("Your relative altitude is {0}", data.RelativeAltitude);

I think just based on these 2 new classes we can tell the context is going to be huge moving forward, whether you think it is an invasion of privacy or an innovation that your mobile device knows how many flights of stair you have claimed that day is a question that only you can answer. All i can say is that as app developers we have lots of options moving forward.

Get the source over at GitHub.

3 thoughts on “Core Motion in iOS 8 and Xamarin

  1. This is a great post Alex and thanks for posting the code snippets as well – i’m looking to dive into Core Motion and it’s great to see what changes Apple have brought with iOS 8!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s