views:

168

answers:

1

Hello all,

I'm trying to implement OAuth securely as detailed here: http://fireeagle.yahoo.net/developer/documentation/oauth_best_practice#custom-url-osx. I seem to have hit a stumbling block, as I am unable to figure out how to handle a url which launches my application when in the background.

I have registered my application to handle oauthtest. I have confirmed that oauthtest:// and oauthtest://callbacktest both launch my application and operate as intended when my application is not running in the background.

I am implementing

application:(UIApplication *) didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

which is successfully called when my application starts up cold. I can easily get the url passed to my application.

However, if my application is already running in the background, neither

application:(UIApplication *) didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

nor

application:(UIApplication *) handleOpenURL:(NSURL *)url 

is called and I have no way of getting the parameters passed to my application as part of the URL.

Does anyone know how to get the parameters passed to a backgrounded application by a custom url scheme?

I am aware that I could work around this issue by disabling multitasking, but I would rather not do that for obvious reasons. Thanks in advance.

+3  A: 

Here's some sample code that seemed to work for me, tested in iOS4:

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
     NSLog(@"handleOpenURL - %@", [url absoluteURL]);
     return YES;    
}

- (void)applicationDidFinishLaunching:(UIApplication *)application {
    NSLog(@"applicationDidFinishLaunching");
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    NSLog(@"didFinishLaunchingWithOptions - %@", [launchOptions objectForKey:@"UIApplicationLaunchOptionsURLKey"]);
    return NO;
}

If I launch the application for the first time, didFinishLaunching: handles the URL. If I then put the app in the background, go back to Safari, and tap a link that brings the app back into the foreground, then handleOpenURL: takes care of the URL. Good luck!

Bob Kressin
Thank you so much for prompting me to look at this again! I was just about to start implementing a complicated workaround. Looking at your code, I realized I was testing handleOpenURL by calling [url query] not [url absoluteURL] with a URL that must not have had valid parameters to return. I foolishly thought that query simply queried the URL for its URL... One quick look at RFC 1808 and I understand the getters for NSURL now. Your code does indeed work wonderfully, thanks again!
ace