9
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 for push notifications. However, when if I send a push notification, the user misses it, and then opens the app via the icon? How can I check when the app was opened from the icon?

nhgrif
  • 61,578
  • 25
  • 134
  • 173
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

1 Answers1

19

The UIApplicationDelegate protocol defines several methods that let you add code to several of your application's life cycle events.

Of specific interest to you would be the following:

  • application(_:willFinishLaunchingWithOptions:) - called just before the application has finished launching when the application was not already active in the background
  • application(_:didFinishLaunchingWithOptions:) - called just after the application has finished launching when the application was not already active in the background
  • applicationDidBecomeActive(_:) - called just after the application has become active, this is called when the user launches from scratch, reopens from background, and also when a user returns from a temporary interruption (such as a phone call)
  • applicationWillEnterForeground(_:) - this is called just before the application enters the foreground after having been in the background--it is immediately followed by the applicationDidBecomeActive(_:) call

This life cycle events can fire whether the user opened your application via a notification or by tapping on the icon. As far as I know there is no way to tell for certain that the application was opened via tapping the icon. You can know(ish) that the application wasn't opened via a notification, as the relevant "did receive notification" methods will never fire. But this still allows the user two (at least) methods of opening the application: tapping the app icon or double tapping the home button and tapping the app to awake it from the background.

nhgrif
  • 61,578
  • 25
  • 134
  • 173
  • I'm guessing "applicationDidBecomeActive" is what I'm looking for right? – TIMEX Apr 19 '15 at 23:29
  • 1
    Probably. It's easy enough to put break points or `println` in all of them and play around to see *exactly* how the life cycle works and figure out what you need exactly. – nhgrif Apr 19 '15 at 23:30
  • According to the reference pages there is a dictionary with keys in which you can find if there are still notifications hanging: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/#//apple_ref/doc/constant_group/Launch_Options_Keys – Simon Apr 20 '15 at 23:19