I’m developing an iphone app using PhoneGap 1.2.0 with Snow Leopard, Xcode 4.2 & running in the iPhone simulator. I connect to a 3rd-party website via Oauth and need to redirect to my app with the attached values. Following Jesse’s guide here, I have the following code:
// 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]; // freezes here
return YES;
}
// JS code loaded in your webview
function handleOpenURL(url)
{
// TODO: do something with the url passed in.
alert(url);
}
The Objective C code lives in “AppDelegate.m” and the JS code is in a separate JS file referenced in index.html. The app freezes at the ‘webView’ line. I believe the issue has to do when something not loading properly – any ideas? When the app is frozen, if I click the iPhone button, then click the app icon, the app reloads and the ‘handleOpenUrl’ JS method runs as desired with the alert.
Found the solution – inspired by this PhoneGap Google group thread. From what I can tell, PhoneGap is executing the “stringByEvaluatingJavaScriptFromString” method before the webview can load, hence the freezing issue. Using the window.setTimeout function, we can ensure that the app has sufficient time to load: