How to check when an application returns to the foreground from the background, but not from a push notification?

func application(application: UIApplication, didReceiveRemoteNotification data: [NSObject : AnyObject]) { var dat = JSON(data) if application.applicationState == UIApplicationState.Active { // app was already in the foreground println("App is in foreground") processNotification(dat) }else{ // app was just brought from background to foreground via PUSH println("App brought back via PUSH") processNotification(dat) } } 

This is how I check push notifications. However, when I send a push notification, the user skips it and then opens the application through the icon? How can I check when the application was opened from the icon?

+5
source share
1 answer

The UIApplicationDelegate protocol defines several methods that allow you to add code to several lifecycle events of your application.

Of particular interest to you will be the following:

  • application(_:willFinishLaunchingWithOptions:) - called just before the application application(_:willFinishLaunchingWithOptions:) , when the application was not already active in the background
  • application(_:didFinishLaunchingWithOptions:) - called immediately after the application has completed launch, when the application was not already active in the background
  • applicationDidBecomeActive(_:) - called immediately after the application has become active, it is called when the user starts from scratch, opens from the background again, and also when the user returns with a temporary interruption (for example, a phone call)
  • applicationWillEnterForeground(_:) - this is called just before the application comes to the foreground after it was in the background - it is immediately followed by a call to applicationDidBecomeActive(_:)

These life cycle events can cause the user to open the application through a notification or by clicking on the icon. As far as I know, there is no way to say for sure that the application was opened by clicking the icon. You may know (ish) that the application was not opened through a notification, since the corresponding methods "received a notification" will never work. But it still allows the user to use two (at least) methods of opening the application: clicking on the application icon or double-clicking the home button and clicking on the application to wake it from the background.

+10
source

All Articles