javascript - 电话间隙 : Custom URL handling freezing

标签 javascript iphone oauth cordova

我正在开发一个 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/

相关文章:

javascript - 语义 UI 下拉选项数据属性

javascript - 在 Pycharm 中,我无法获取 JSON 文件并且无法安装 fetch

javascript - Opera 用户-JS : how do I get the raw server response?

oauth - google oauth2 grant_type=authorization_code 返回错误处理 OAuth 2 请求

javascript - Axios CORS 问题与 Github oauth 未获取访问 token

python - 用 Python 编写 OAuth 提供程序 : guide + libraries?

javascript - 使用 AJAX 解析从 ASP 页面返回的 HTML 表并提取特定单元格

iphone - Xcode 项目的持续集成?

iphone - 更改 uiwebview div 对象的内容

iphone - 使用两个 View 的图标选择器