gpt4 book ai didi

javascript - 电话间隙 : Custom URL handling freezing

转载 作者:行者123 更新时间:2023-11-28 21:13:09 26 4
gpt4 key购买 nike

我正在开发一个 iPhone 应用程序,使用 PhoneGap 1.2.0 和 Snow Leopard、Xcode 4.2 并在 iPhone 模拟器中运行。我通过 Oauth 连接到第 3 方网站,需要使用附加值重定向到我的应用程序。跟随杰西的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 方法将根据需要运行警报。

最佳答案

找到了解决方案 - 受到此 PhoneGap 的启发 Google group thread 。据我所知,PhoneGap 在 webview 加载之前执行“stringByEvaluatingJavaScriptFromString”方法,因此出现卡住问题。使用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;
}

关于javascript - 电话间隙 : Custom URL handling freezing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8204308/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com