4

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!

FS.O6
  • 1,394
  • 2
  • 20
  • 42

3 Answers3

5

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.

Ravi Ojha
  • 1,480
  • 17
  • 27
Mykyta Savchuk
  • 1,195
  • 13
  • 13
  • 2
    But is it working good on the background? Without draining the battery too much? – FS.O6 Oct 25 '16 at 12:28
  • You can simply use delay for this request, and it wouldn't drain too much battery. Or connect requests with changing geoposition, so you would request activity type when your change geoposition. – Mykyta Savchuk Oct 25 '16 at 12:31
  • what do you mean? Can you explain a bit more? – FS.O6 Oct 25 '16 at 12:33
  • For example you can save current position, and after passing 10 meters (or more) you can request activity type. So you can make request every 10 meters for example. You just have to set value after you would reach it, activity type would be requesting. Or you can set timer and request activity type every minute/10 minutes/hour – Mykyta Savchuk Oct 25 '16 at 12:40
  • I must say this is optimum answer, from m7 processor you will get pedometer. Using pedometer you will get all information of walking, true king etc. using below answer of UIBackGround modes you can keep updating though data on server using API. – Hasya Oct 30 '16 at 07:58
  • 2
    @MykytaSavchuk@Ravi Ojha: Please read question carefully. Question is related to background mode and your giving unrelated answer. Please let us know if you know how to get motion in background mode and update you answer. Thank you...!! – Sagar Sukode Nov 22 '18 at 10:48
  • Since isActivityAvailable() is a type method, you should do it this way: CMMotionActivityManager.isActivityAvailable() – jamryu Jul 25 '21 at 20:37
1

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
Rahul
  • 241
  • 1
  • 12
  • But how do I do that? Where can I get the information like `if(userIsWalking) { print("You are walking") }` ? – FS.O6 Oct 25 '16 at 11:38
  • If you use VoIP configuration and your app doesn't support such functionality then Apple can reject it. – Hasya Oct 30 '16 at 07:54
1

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.

Rashwan L
  • 38,237
  • 7
  • 103
  • 107