Search code examples
iosobjective-cios5ios6

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.


Solution

  • 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.