36

NOTICE: This question was originally posted before Apple introduced motion-detection hardware and associated APIs in the iOS SDK. Answers to this question, however, remain relevant.


I'm creating an iPhone iOS app which involves tracking a user's running and / or walking. It is very important that the recorded results of the users runs and walks remain honest. I need a way to catch a user who may be cheating (or accidentally have left the tracker on) when using a car.

To check if the user is driving or riding in a car I first thought of these two checks, however neither can really determine if the user is in a car or not (to a point).

  1. Check the user's current speed in the following method. If the user is traveling faster than 20-ish MPH, then I could assume that the user is in a car:

    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
          CLLocation *recentLocation = [locations lastObject];
          recentLocation.speed; //If speed is over 20 MPH, assume the user is not on their feet.
    

    However, I'm not sure if this is really a good check because people have been recorded to go faster than that before. Is this a good check or should I use something else?

  2. Determine how fast the user is accelerating using the Accelerometer and Motion APIs provided with the Core Motion Framework. If the user accelerates over a certain rate, then I could assume the user is traveling in a vehicle.

Are these assumptions correct? I guess my real question is this: Is there any better way to detect if the user is moving in a vehicle - if so how?. And if not, then are these checks suitable for this case or would this just be annoying to some users who are actually that fast? Is CoreMotion the proper API to do this with?


EDIT: The new iPhones 5S M7 Coprocessor provides more accurate movement detection. Could anyone explain how to use the new M7 APIs?

8
  • Planning on selling it to law enforcement? :D
    – jdero
    Jul 25, 2013 at 22:12
  • 4
    Maybe use the accelerometer to detect the repeating jars from footsteps.
    – Lee Meador
    Jul 25, 2013 at 22:16
  • @LeeMeador That's actually a really good idea Jul 25, 2013 at 22:22
  • 1
    If you use speed, you'll need to average. I regularly ride my push-bike at 40+ mph, but usually not for long periods of time ;-)
    – Wain
    Jul 25, 2013 at 22:23
  • 1
    @Wain you must be pushing really hard to get up to that sort of speed :)
    – Abizern
    Nov 20, 2013 at 18:26

4 Answers 4

31

All the advice about the wisdom (or lack thereof) in guessing about motion states from location data still applies. But regarding your update about Core Motion and M7...

Yes, you can use Core Motion on devices with an M7, M8, M9, etc motion coprocessor(*) to get an indication of whether the user might be driving.

  1. Create a CMMotionActivityManager object (after using its class method isActivityAvailable to determine whether you you have M7(+) features), and either start activity updates or query it for recent activities.
  2. Check the returned CMMotionActivity objects' automotive property to see if iOS thinks the user is/was in a car.
  3. There's no step three.

Like the GPS inferences, though, you should still take this information with a grain of salt. CoreMotion APIs give you iOS' best guess as to the user's activity, but there's no guarantee it's 100% accurate. (For example, I'm not sure if riding a train might count as automotive. Also note that the different activity types are not mutually exclusive.) It's better for your app to check for the activity types you're interested in than to try to exclude the ones you don't want.


(*) M7 devices are those with the A7 SoC: iPhone 5s, iPad Air, iPad mini 2. M8 is A8, M9 is A9, etc. In short, every iOS device introduced since Fall 2013, excluding iPhone 5c.

1
  • CMMotionActivityManager is for M7 only. As other answers suggest, you can attempt to guess motion state from location and (non-M7) motion data, but caveat emptor.
    – rickster
    Apr 25, 2014 at 17:09
15

You can use this simple library to detect if user is walking, running, on vehicle or not moving.

Works on all iOS devices and no need M7 chip.

https://github.com/SocialObjects-Software/SOMotionDetector

In repo you can find demo project

9
  • This sample gives state based on locationmanager's speed, not using the reading of coremotion. CLLocationmanger won't give accurate speed Mar 19, 2014 at 10:19
  • 1
    You are wrong. This sample calculates motion type by using 2 factors: speed (CoreLocation) and device acceleration (CoreMotion). You can easily make sure to this, just build demo project to your device, go outside and RUN. You can run with slow speed, but your motion type will be detected as Running instead of walking because of higher acceleration of your device :)
    – arturdev
    Mar 19, 2014 at 11:56
  • 1
    you should test this app while turn off location service. Then you come to know, how this app is detecting user activity. Without location service not motion detection here. Only shaking is detected by app. One this if you turn on location service continuously device is a major concern. Apr 9, 2014 at 19:53
  • 2
    Appreciating you work. But this is not the correct way to detect user activity. CLLocationManager won't give exact speed. And you are deciding user activity based on device speed. Apr 10, 2014 at 9:51
  • 1
    This library sets locationManager.desiredAccuracy = kCLLocationAccuracyBest and in my experience that does give speed readings with decent accuracy but also consumes a lot of battery. For higher accuracy (worse off) CLLocationManager won't give speed readings.
    – stefann
    May 3, 2014 at 1:29
3

The CLLocation based check is the only reliable information you can get. As specified by Ali in Need to find Distance using Gyro+Accelerometer it is useless to find velocity and position for a longer period of time. The integrated acceleration values start drifting after 0.5 - 2 seconds and there is no chance to get them calibrated again.

Depending on your use case I see some more problems than Usain Bolt's 44.72 km/h (27.79 mph):

  1. Regions with no GPS signal like tunnels, underground parking lot, ...
  2. Errors in GPS. I remember a 6 hours tracking tour in the German Alps and this is what the app thought about it :-)
    Walking on water with 920 km/h
  3. As you mentioned already, you never know if the user is in car, train, underground, bus, ... And you never know if he is driving himself or just the co-driver - if this matters.
2
  • +1 By the way, I started a write-up on the subject. These sort of questions pop up too often. Unfortunately, I am way too busy these day so I have no idea when I will be finished with the write-up. Congrats on the iPhone badge!
    – Ali
    Jul 29, 2013 at 21:10
  • @Ali :-) You just reminded me of my promise to write a posting and a SO FAQ about displacement. But I just started the testing phase of my 'totally motion controlled' iPhone game. Feedback was great but it will take another month to get it bullet proof and then ... ;-)
    – Kay
    Jul 29, 2013 at 22:11
2

Xcode 7.3, iOS 9.3

Thanks to all the original contributors.

I just spent a few hours trying to understand why this fairly straight forward class was not working for me. I used all the suggestion above and from other posts to see what activity iOS thought the user was performing using Core Motion framework and using the CMMotionActivityManager class.

Turns out that for whatever reason I had my "Fitness Tracking" in "Settings"->"Privacy"->"Motion & Fitness" turned off. And for whatever reason after adding the framework and calling a CM class, the app. did not request to enable this. I must have turned this off manually.

Eventually I stumbled onto this post...

iOS - is Motion Activity Enabled in Settings > Privacy > Motion Activity

Which lead me to check the setting and resolve the issue. After I flipped "Fitness Tracking" to on and ran the app. again, everything worked like a charm.

Hopefully this saves someone some time! Cheers.

3
  • I don't suppose you found a way to check if "Fitness Tracking" is enabled in iOS settings programmatically?
    – Alex Hall
    Jun 29, 2017 at 17:56
  • 1
    @AlexHall Hey, as far as I know there is no direct way to check this. But you can check if you are authorized to utilize CMMotionActivityManager, e.g. "CMErrorMotionActivityNotAuthorized". And if you are not, then write some code to handle this error to alert the user to do something, e.g. turn "Fitness Tracking" on. Check out the post I linked above.
    – serge-k
    Jun 30, 2017 at 0:45
  • It have trouble getting it to work properly in the background. Does it work robustly in the background
    – RawMean
    Oct 11, 2022 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.