1

I’m trying to make a focus timer app like Forest, that detects if you leave the app, either by going to the home screen, switching to another app or by first locking the phone and later proceeding to a different app through notifications, widgets, camera etc..

What I can’t figure out is how to monitor such state changes even after the phone has been locked for a while. There are no “background modes” covering this use case, so I would expect the app to get suspended after a while in the background. Nevertheless, apps like Forest do this successfully. Do I need a workaround to keep the app awake, or am I missing some approach that doesn’t require background execution at all?

Help much appreciated!

1 Answers1

0

If I had to guess, it's probably a combination of a few things:

  1. Use the normal app delegate callbacks for app state transitions to determine if the app is still able to execute code. That's the easy part.
  2. You could do something with string and regex processing of console logs to detect when other apps open, close, etc. and other activity which would allow you to detect activity on the device coming from places other than your app.
  3. You need to use some sort of framework that Apple says can get data even in background state. One such framework is Core Motion. There may be others as well that suit your app's specific needs better.

See Execution States for Apps.

See Background Execution.

See Cocoanetics: Accessing the iOS System Log.

See Keep iOS App Awake To Monitor Movement.

See Apple System Log Facility.

Jacob M. Barnard
  • 1,347
  • 1
  • 10
  • 24