FS.O6
FS.O6

Reputation: 1464

Is it possible to detect the user's moving activity on the background?

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!

Upvotes: 5

Views: 5520

Answers (3)

Mykyta Savchuk
Mykyta Savchuk

Reputation: 1225

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.

Upvotes: 7

Rashwan L
Rashwan L

Reputation: 38833

Yes it´s possible to do that!

If your iOS app must keep monitoring location even while it’s in the background, use the standard location service and specify the location value of the UIBackgroundModes key to continue running in the background and receiving location updates. (In this situation, you should also make sure the location manager’s pausesLocationUpdatesAutomatically property is set to YES to help conserve power.) Examples of apps that might need this type of location updating are fitness or turn-by-turn navigation apps.

Read more here.

Upvotes: 1

Rahul
Rahul

Reputation: 241

Regarding the user activity which can be handled in background tasks are the below once which does not mention about (walking, cycling,driving etc...)

Implementing Long-Running Background Tasks

For tasks that require more execution time to implement, you must request specific permissions to run them in the background without their being suspended. In iOS, only specific app types are allowed to run in the background:

  • Apps that play audible content to the user while in the background, such as a music player app
  • Apps that record audio content while in the background.
  • Apps that keep users informed of their location at all times, such as a navigation app Apps that support Voice over Internet Protocol (VoIP)
  • Apps that need to download and process new content regularly
  • Apps that receive regular updates from external accessories

Upvotes: 1

Related Questions