iPhone: applicationDidFinishLaunching & handleOpenUrl

    November 3rd, 2008 Posted by: - posted under:Snippets

    Problem:

    You use the applicationDidFinishLaunching method to kick off your application. This event fires automatically on your delegate whenever your app launches.

    If your app launches from a special url schema (tel://, http://, mailto://), then another event is fired:

    handleOpenUrl

    As you might have noticed in the LaunchMe sample project that ships with Xcode, these two methods will most likely conflict.

    Solution:

    Move the functionality from applicationDidFinishLaunching and put it in another method, like postLaunch. Then add a member variable to the application delegate to keep track of how the app started:


    @interface YourAppDelegate : NSObject {
    BOOL launchDefault;
    }

    @property BOOL launchDefault;

    Then put this in applicationDidFinishLaunching:


    launchDefault = YES;
    [self performSelector:@selector(postFinishLaunch) withObject:nil afterDelay:0.0];

    Then in handleOpenUrl, add this:


    launchDefault = NO;

    Lastly, write the postLaunch method and make it check the launchDefault variable.


    - (void)postLaunch{
    if (launchDefault){
    // Add your functionality from applicationDidFinishLaunching here.
    }else{
    // don't do anything....let handleOpenURL run
    }

    }

    How does this work?

    You’re effectively adding a method to the event chain to be called after all other events. You’re putting a 0 second delay on it, so it shouldn’t add any extra time to the overall loading time.