What method is called before the screen goes black

2019-06-05 03:55发布

问题:

This question already has an answer here:

  • iPhone: Detecting user inactivity/idle time since last screen touch 9 answers

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 and I had difficulty Googling the right terms (e.g., "method inactivity app objective c"). I also didn't see anything on Stack Overflow.


EDIT / CLARIFICATION: Multiple people have 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. Please let me know what I am misunderstanding if my understanding is off.

  • 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?
  • Other 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 assume that the screen is "off" after 1 minute of inactivity, but this to me would be a less-than-desireable assumption.

回答1:

As far as I know, there is no such an method. Only ones you mentioned.



回答2:

This one is invoked when the app is about to become inactive (you shouldstop any process here like timers etc..)

[UIAppDelegate applicationWillResignActive:]

when it has already become inactive (you should save any important data here)

-[UIAppDelegate applicationDidEnterBackground:]

when your app is killed (save all , kill em all)

-[UIAppDelegate willTerminate:]

for any other info.. you can check this link

https://developer.apple.com/library/ios/documentation/uikit/reference/UIApplicationDelegate_Protocol/Reference/Reference.html