Skip to content

Instantly share code, notes, and snippets.

@purplecabbage
Created March 7, 2011 23:47
Show Gist options
  • Save purplecabbage/859540 to your computer and use it in GitHub Desktop.
Save purplecabbage/859540 to your computer and use it in GitHub Desktop.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// perform any custom startup stuff you need to ...
// process your launch options
NSArray *keyArray = [launchOptions allKeys];
if ([launchOptions objectForKey:[keyArray objectAtIndex:0]]!=nil)
{
// we store the string, so we can use it later, after the webView loads
NSURL *url = [launchOptions objectForKey:[keyArray objectAtIndex:0]];
self.invokeString = [url absoluteString];
NSLog(@amp;" launchOptions = %@",url); // if you want to see what is happening
}
// call super, because it is super important ( 99% of phonegap functionality starts here )
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
// Objective-C code in your AppDelegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
// Do something with the url here
NSString* jsString = [NSString stringWithFormat:@"handleOpenURL(\"%@\");", url];
[webView stringByEvaluatingJavaScriptFromString:jsString];
return YES;
}
// JS code loaded in your webview
function handleOpenURL(url)
{
// TODO: do something with the url passed in.
}
function onDeviceReady()
{
// do your thing!
// at this point, the page is loaded and PhoneGap is ready to rock.
// and if we have been launched with a protocol, we can access it via the invokeString variable.
if(invokeString)
{
alert("I got a freaking invokeString :: " + invokeString);
}
}
- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
// only valid if ___PROJECTNAME___.plist specifies a protocol to handle
if(self.invokeString)
{
// this is passed before the deviceready event is fired, so you can access it in js when you receive deviceready
NSString* jsString = [NSString stringWithFormat:@"var invokeString = \"%@\";", self.invokeString];
[theWebView stringByEvaluatingJavaScriptFromString:jsString];
}
return [ super webViewDidFinishLoad:theWebView ];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment