0

I'd like to perform some methods before my app closes due to user inactivity (i.e., the screen going black and the phone locking when you haven't touched it for a while). What method(s) is/are called when this happens?

I'm guessing:

  • This method / these methods would be part of my AppDelegate class.
  • This method / these methods would be analogous to -[UIAppDelegate applicationWillResignActive:], -[UIAppDelegate applicationDidEnterBackground:], -[UIAppDelegate willTerminate:].

I didn't see anything in the documentation for UIApplicationDelegate regarding methods or practices for triggering methods when the screen turns off. Likewise, Googling terms such as "method inactivity app screen off objective c" also didn't turn anything up.

In a prior incarnation of this question (What method is called before the screen goes black after inactivity?), multiple people directed me towards the following Stack Overflow post as a possible duplicate: iPhone: Detecting user inactivity/idle time since last screen touch. However, I do believe my question is very different:

  • My question: Is there a built-in method in Objective-C akin to -[UIAppDelegate applicationWillResignActive:] et al. that is called or property that is changed when the screen turns black after a period of inactivity while an app is still open? If not, how can I go about implementing a similar logic without such a method?
  • Other user's question: How can I trigger a method after a predetermined period of user inactivity, regardless of screen state?

I could see how the other question could answer my question if there is a method for obtaining the length of time the user has set for "Auto-Lock" under Settings (either 1 min, 2 min, 3 min, 4 min, 5 min, or Never), but I don't see such a method or property in the documentation for UIApplication. I suppose a compromise would be to check if the app is still running every 1 minute of inactivity, with the hypothesis/assumption that this loop will cease executing when the screen turns off, but this to me would seem to be a less-than-optimal solution.

The reason I am asking this question is because I am looking to keep track of when my user is using my app and for how long. I'm creating a very basic Core Data database of sessions that includes when a session begins and when it ends. Since I haven't found a way to determine when the screen goes black due to user inactivity, my data is currently inaccurate.

Community
  • 1
  • 1
Ken M. Haggerty
  • 24,902
  • 5
  • 28
  • 37

2 Answers2

2

There is a 'UIApplicationDidEnterBackgroundNotification' notification that you can listen to to know when you're app is entering the background. You can register as follows and handle it in the selector that you indicate here:

[[NSNotificationCenter defaultCenter] addObserver:someObject 
                                         selector:@selector(someMethodName:) 
                                             name:@"UIApplicationDidEnterBackgroundNotification" object:nil];

Similarly you can listen to the 'UIApplicationDidEnterForegroundNotification' to know when your app is back in foreground.

tanz
  • 2,557
  • 20
  • 31
0

As far as I know, when your device is locked, your app is handled the exact same way as if it was paused and sent to the inactive background apps. I feel this is simple state preservation/restoration issue (so timers won't be necessary). Check the link below about states: Apple's Guide on states

blindman457
  • 293
  • 1
  • 11