Doug Smith
Doug Smith

Reputation: 29326

Every time my app launches (whether from cold launch or background) I want to have it do something, how do I accomplish this?

I want to check every time the app launches whether or not there's a URL in the clipboard, and if so, do something with it. Which method fires that I can override whenever the app launches, whether from a cold launch (it was killed in the background for instance) or if I just press the home button, copy a URL and jump back in.

Is it one of these?

 - (void)applicationDidBecomeActive:(UIApplication *)application
 - (void)applicationWillEnterForeground:(UIApplication *)application
 - (void)applicationDidBecomeActive
 - (void)applicationDidFinishLaunching:(UIApplication *)application

Confused.

Upvotes: 4

Views: 4614

Answers (4)

AdamG
AdamG

Reputation: 3718

In your app delegate add it to the methods that the other answers have suggested (applicationDidFinishLaunchingWithOptions:). In your root view controller register for the following notification. This will always be called when your application launches once it has already started running.

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bringingItBack)      name:UIApplicationWillEnterForegroundNotification object:nil];

This will cover both the instances when the app launches and when you are just bringing it back from the background.

Upvotes: 0

davidf2281
davidf2281

Reputation: 1328

As @rmaddy says, the correct method to use once the app launched is applicationWillEnterForeground: from your app delegate. This method will be called when the user jump backs in, but NOT in other circumstances you don't need to respond to (such as the user receiving a text message and dismissing it).

However, from my testing, applicationWillEnterForeground: is not called when an app is launched from cold; you should catch that in applicationDidFinishLaunchingWithOptions:.

So, basically, your app delegate should include code like this:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    [self checkForURL];
    ...
}


- (void)applicationWillEnterForeground:(UIApplication *)application {

       [self checkForURL];
       ...
}

- (void)checkForURL{
    //code for checking for URL goes here
}

Hope that helps.

Upvotes: 16

JainAnk
JainAnk

Reputation: 204

With reference to the UIApplicationDelegate Protocol, the handling of app launches can be handled in 2 methods:

  • application:willFinishLaunchingWithOptions:
  • application:didFinishLaunchingWithOptions:

And the handling of app launches from background can be handled with the help of method: applicationDidBecomeActive:

Based on the above call, you can handle your application.

Upvotes: 0

nathancahill
nathancahill

Reputation: 10850

- (void)applicationDidBecomeActive is called when the app is launched or becomes active from the background.

This doc explains everything pretty well: http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

Upvotes: 0

Related Questions