In iOS application development, iOS motion activity detection with Core Motion is one of the most powerful tools for tracking and analyzing user movement. Through this API, we can access information about various types of user activities, such as walking, running, and even car travel. This capability is valuable for a wide range of applications, from physical activity tracking to navigation or transportation applications.
However, despite its many advantages, Core Motion is not always perfect. In this post, we will explore the different types of motion activity that Core Motion can detect, its practical applications and common problems that developers might encounter, such as incorrect activity detection and confusion between Cycling and Automotive. We will also see how to query Core Motion for historical activity data.

Types of Activities that Core Motion can detect
Core Motion can detect several types of activities through its CMMotionActivityManager API. These are the main types of activities that can be recognized:
- Stationary: The device detects that the user is still, i.e., not moving significantly. This is useful for applications that need to know when the user is resting or remains in a fixed position.
- Walking: It detects when the user is walking. This is useful for health and fitness applications, as it can be used to calculate steps and distance traveled.
- Running: Similar to walking, but detects when the user is running. Ideal for fitness tracking applications that track different levels of physical activity.
- Automotive: It detects when the user is traveling in a motorized vehicle. This is key for navigation applications, as it can determine if the user is traveling in a car, rather than walking or running.
- Cycling: Detects when the user is riding a bicycle. It is important for applications focused on cycling or physical activity.
- Unknown: In some cases, Core Motion cannot confidently determine the type of activity, so it classifies the activity as “Unknown”.
Why These Activity Types Are Useful
Using Core Motion's activity detection can improve the functionality of many applications, especially those focused on health, fitness and navigation.
- Health and Fitness: Allows applications to track and measure physical activity, steps, calories burned and more.
- Navigation: For mapping and navigation applications, knowing whether the user is walking, running, cycling or driving helps to provide the most relevant directions and appropriate information.
- Contextual Experience: Knowing what activity the user is doing allows you to customize the behavior of the application. For example, you could offer bike-friendly routes or adjust fitness challenges based on whether the user is running or walking.
Common Challenges in Motion Activity Detection
Although Core Motion provides valuable data, it is not always perfect. There are several challenges that can arise, especially when activity transitions occur or when the system confuses Cycling and Automotive. Let's look at the most common problems.
Activity Transitions: Activity “Unknown”.”
One of the most common problems occurs when the user quickly changes from one activity to another. For example, if someone is walking and suddenly starts running, Core Motion may not detect the change correctly and classify the activity as “Unknown” for a short period of time.
This behavior can be especially frustrating for fitness applications, as accurate activity tracking is crucial. If Core Motion incorrectly classifies the activity as “Unknown”, the data may not be useful for tracking fitness progress.
2. Incorrect detection between “Cycling” and “Automotive”.”
Another common problem is confusion between “Cycling” and “Automotive”. In situations where the user is traveling at a similar speed (e.g., riding a bicycle on the highway and traveling in a car with heavy traffic), Core Motion may incorrectly label the activity as “Cycling” instead of “Automotive” (or vice versa).
This issue is particularly relevant for applications that rely on accurate mode detection to provide relevant routing information or adjust the user interface. The difference between Cycling and Automotive can affect directions, estimated arrival times and other metrics within the application.
3. Latency in the Detection of New Activities
There may also be latency when Core Motion detects a change in activity. If the user changes activity, there may be a short delay before the new mode is correctly recognized. This latency could be a drawback if you need real-time updates for a smooth experience.
Core Motion Queries: How to Obtain Historical Activity Data
A useful feature offered by Core Motion is the ability to query past activity data. If you don't need real-time data and want to analyze user behavior during a specific time period, you can use the queryMotionActivity(from:to:) method to get historical data about user activities.
Example of a query of past activities:
let motionActivityManager = CMMotionActivityManager()
// Define the time range
let startDate = Date().addingTimeInterval(-3600) // 1 hour ago
let endDate = Date()
// Perform the activity query
motionActivityManager.queryMotionActivity(from: startDate, to: endDate) { (activities, error) in
guard let activities = activities else { return }
// Process retrieved activities
for activity in activities {
print("Activity: \(activity.activityType), Confidence: \(activity.confidence.rawValue)")
}
}This method is useful if you want to analyze how a user moved during a given period, reviewing, for example, the user's activity throughout the day.
Conclusion
Core Motion is a powerful tool for detecting and analyzing user movement, offering great potential for building applications that track physical activity, navigation, and more. However, like any tool, it has its limitations, such as confusion between activity types, and occasional classifications of activities as “Unknown”. Developers must be aware of these complexities and take them into account when designing our applications, ensuring that users receive accurate and useful data.
Despite these challenges, Core Motion provides valuable information that can significantly improve the user experience. By understanding its limitations and combining it with other features, such as GPS and Location Services, we can build more accurate and efficient applications. In addition, querying past activity allows us to observe user behavior over time, opening up new possibilities for analysis.
I invite you to try it in your app and, if you want to save time, here is an example to experiment with.
The latest updates can be found here
Read more in our blog
