我正在使用带有Snow Leopard的PhoneGap 1.2.0开发一款iPhone应用程序,Xcode 4.2&在iPhone模拟器中运行。我通过Oauth连接到第三方网站,需要使用附加的值重定向到我的应用程序。在Jesse的guide here之后,我有以下代码:
// 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);
}
Objective C代码位于“AppDelegate.m”中,JS代码位于index.html中引用的单独JS文件中。该应用程序冻结在'webView'行。我相信当问题没有正确加载时,问题必须做 - 任何想法?当应用程序被冻结时,如果我单击iPhone按钮,然后单击应用程序图标,应用程序重新加载,并且'handleOpenUrl'JS方法根据需要运行警报。
答案 0 :(得分:4)
找到解决方案 - 受此PhoneGap Google group thread的启发。据我所知,PhoneGap正在执行“stringByEvaluatingJavaScriptFromString”方法,然后才能加载webview,因此出现了冻结问题。使用window.setTimeout函数,我们可以确保应用程序有足够的时间加载:
// Objective-C code in your AppDelegate
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
// Do something with the url here
NSString* jsString = [NSString stringWithFormat:@
"window.setTimeout(function() { \n"
"handleOpenURL(\"%@\"); \n"
"},1);", url];
[webView stringByEvaluatingJavaScriptFromString:jsString];
return YES;
}
答案 1 :(得分:0)
我相信我的一位同事曾经提到过包含“ - ”字符的网址引起了问题,网址中是否有“ - ”或“_”?
答案 2 :(得分:0)
自定义URL处理现在是Cordova库的一部分,请参见此处: Cordova Custom URL Scheme Handling
答案 3 :(得分:-1)
使用Cordova 1.7.0,AppDelegate.m中的黑客代码现在看起来像这样:
// this happens while we are running ( in the background, or from within our own app )
// only valid if ProchainBus-Info.plist specifies a protocol to handle
// HACKED - stackoverflow.com/questions/8204308/phonegap-custom-url-handling-freezing
- (BOOL) application:(UIApplication*)application handleOpenURL:(NSURL*)url
{
if (!url) {
return NO;
}
// calls into javascript global function 'handleOpenURL'
NSString* jsString = [NSString stringWithFormat:@
"window.setTimeout(function() { \n"
"handleOpenURL(\"%@\"); \n"
"},1);", url];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];
// all plugins will get the notification, and their handlers will be called
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
return YES;
}