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