iPhone: applicationDidFinishLaunching & handleOpenUrl

  • Twitter
  • Facebook
  • Digg
  • Reddit
  • StumbleUpon
  • del.icio.us
  • Google Bookmarks
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.

  • Hombre

    Just the info that I needed.
    Thanks!

  • DadGuy

    Thanks for this, I wasn’t aware that you could schedule methods that way. I’ve used a NSTimer before to do the same thing.

    I ran into some issues with URL handling and appreciate the post!

  • Denis Altudov

    Josh thank you so much for this nugget!

  • Graham Dawson

    Thanks. Very helpful post! However, an issue I now have is that the splash screen (Default.png) still disappears after applicationDidFinishLaunching, whereas I want its disappearance to occur after postLaunch instead, to avoid a blank screen between the splash and the appearance of the initial app view. Any ideas on how to remedy this?

  • Graham Dawson

    Found a solution myself – see http://michael.burford.net/2008/11/fading-defaultpng-when-iphone-app.html – I just adapted the code to display a view with a copy of the splash until initial processing had finished. :-)

  • http://www.uwsp.edu/ATHLETICS/mbb/06-07/index.htm Pointer Men’s Basketball

    Best you could edit the post subject title iPhone: applicationDidFinishLaunching & handleOpenUrl | iCodeBlog to more catching for your webpage you write. I loved the post all the same.

blog comments powered by Disqus