How do I know when an app goes into the background?

As far as I know, there are two ways an app can move in the background:

1) User pressed the home button

2) The application remained for a while without any action from the user, and the device falls asleep

These two results call the same method applicationDidEnterBackground

and the application state becomes UIApplicationState.Background

. So how do I know how this happened? I know this is possible because I have an application in front of my eyes that takes different actions based on whether the home button is pressed or not. Any ideas guys?

+3


source to share


1 answer


As far as I know (trust me I tried ) there is no official public API that allows you to do this. However, I found this GitHub repo , the file you should pay special attention to is the application delegation file .

Repo says:

This project describes how to detect when the user presses the home button to leave your app, and when they hit the lock button to put your app in the background.

It uses CSNotificationCenter to detect Darwin's notifications.

This user answered two Stack Overflow questions related to this, which the repo seems to be based on:



Separation between lock screen and home button press on iOS7

How to distinguish between lock screen and home button on iOS5?

To be honest, I don't know if Apple can refuse or accept this implementation. Let me know if you use it in any way. I also believe this is iOS 7 and up.

+1


source







All Articles