Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 660
FS.O6 Avatar asked Mar 11 '23 20:03

FS.O6


2 Answers

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.

like image 80
Mykyta Savchuk Avatar answered Apr 26 '23 03:04

Mykyta Savchuk


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
like image 41
Rahul Avatar answered Apr 26 '23 05:04

Rahul