Andrey Chernukha
Andrey Chernukha

Reputation: 21818

How to recognise the way the app goes into background?

As far as I know by now there are two ways an app can move into background:

1) User pressed home button

2) The app has been left for a while without any action from user and device fell asleep

These two result in one - applicationDidEnterBackground method is called and application state becomes UIApplicationState.Background. So how do I know how exactly this happened? I know that it's possible because I have an app before my eyes which takes different action based on whether user pressed home button or not. Any ideas, guys?

Upvotes: 3

Views: 510

Answers (1)

MCKapur
MCKapur

Reputation: 9157

To my knowledge (believe me, I've tried), there is no formal public API allowing you to do this. However, I found this GitHub repo, the file you should specifically look at is the app delegate implementation file.

The repo says:

This project describes how to detect when a user presses the home button to leave your application and when they press the lock button to put your app into the background.

It utilizes CSNotificationCenter to detect Darwin notifications.

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

Differentiate between screen lock and home button press on iOS7

How to differentiate between screen lock and home button press on iOS5?

I honestly have no clue if Apple would reject or accept this implementation. Do let me know if you end up using it somehow. I also believe this is iOS 7 and up.

Upvotes: 1

Related Questions