Is it possible to detect the user's moving activity on the background?
algorithm, cocoa-touch, ios, objective-c, swift
Solution
There is coprocessor m7(+) in iPhones upper 5s. It gives you possibility to get device motion. Just
import CoreMotion
in your file.
Create a CMMotionActivityManager object:
let motionActivityManager = CMMotionActivityManager()
Check if it`s available on your device: `motionActivityManager.isActivityAvailable()`
Use this method:
motionActivityManager.startActivityUpdates(to: OperationQueue.main) { (activity) in
if (activity?.automotive)! {
print("User using car")
}
if (activity?.cycling)! {
print("User is cycling")
}
if (activity?.running)! {
print("User is running")
}
if (activity?.walking)! {
print("User is walking")
}
if (activity?.stationary)! {
print("User is standing")
}
if (activity?.unknown)! {
print("Unknown activity")
}
}
It would return you types of user activity.
Problem
I want to develop an app that detecting the user's moving way (walking, cycling, driving etc...) and send a specific `UILocalNotification` for each activity type. My question is: is it possible to detect it on the background (when the app is completely closed) without draining the device's battery? What will be the best way to do it? Thank you!