1

I want to perform different actions based on if my app was launched from the background, or if it was launched and it wasn't in the background. From what I have read, this can be done in the

func application(application: UIApplication!, willFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool

function. I am able to determine if the app is launching for the first time ever by using this:

if NSUserDefaults.standardUserDefaults().boolForKey("FirstLaunch") == true
    {
        NSUserDefaults.standardUserDefaults().setBool(false, forKey: "FirstLaunch")
        NSUserDefaults.standardUserDefaults().synchronize()
        println("false")
    }
    else
    {
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: "FirstLaunch")
        NSUserDefaults.standardUserDefaults().synchronize()
        println("true")
    }

But I am not able to determine from what state the app is becoming active.

UPDATE

I gave Drewag credit because you got me on the right track. You were correct on the didFinishLaunchingWithOptions.

Ok after testing some of your suggestions it seems it works like this:

If application is opened, regardless of if it is in the background or not fires:

 applicationDidBecomeActive

So that one is not helpful. However, when I launch the application and it was NOT in the background, that seems to be the only time that these fire:

didFinishLaunchingWithOptions
willFinishLaunchingWithOptions

And when the application is opened while in the background from any of these three scenarios:

1.) Double clicking the home button and selecting the app window 2.) Clicking the app icon from the springboard 3.) Opening the application from a UILocalNotification click

This fires:

applicationWillEnterForeground
PretzelJesus
  • 869
  • 3
  • 11
  • 21
  • I don't know if this is possible, or at least it hasn't been since iOS 5. Perhaps there is a way to determine what caused the app to quit (home or lock button)? If you catch this, you could save that to NSUserDefaults and launch accordingly on the next launch. A solution here (http://stackoverflow.com/questions/19452696/detecting-when-app-is-becoming-active-from-lockscreen-vs-other-on-ios7) suggested checking the screen brightness, which doesn't work anymore and was a hack to begin with. – timgcarlson Jul 12 '14 at 17:09

2 Answers2

3

just graphic addition to the correct answer:

iOS app lifecycle

from apple developer documentation

Ben
  • 3,832
  • 1
  • 29
  • 30
1

That is the difference between application:didFinishLaunchingWithOptions: and applicationDidBecomeActive:.

application:didFinishLaunchingWithOptions: is only called when the application is starting new.

applicationDidBecomeActive: will be called when the app is becoming active again from the background. Note: applicationDidBecomeActive: is called on first launch as well. If you need to differentiate between the two cases, you have to do so using application:didFinishLaunchingWithOptions:. You could set a local variable to track if it was launched for the first time.

drewag
  • 93,393
  • 28
  • 139
  • 128
  • 1
    NOTE: if you app was killed by iOS, it still appears in BG but will be launched (and not just become active) as well. – Daij-Djan Jul 12 '14 at 18:36