【问题标题】:Phonegap: Custom URL handling freezingPhonegap:自定义 URL 处理冻结
【发布时间】:2012-01-02 12:12:59
【问题描述】:

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

【问题讨论】:

  • 我目前遇到了同样的问题,想要一个解决方案。目前仅在模拟器中测试。
  • @MarcusJoe 希望我的回答对您有所帮助!

标签: javascript iphone oauth cordova


【解决方案1】:

自定义 URL 处理现在是 Cordova 库的一部分,请参见此处: Cordova Custom URL Scheme Handling

【讨论】:

  • 从 2.3 开始仍然需要添加 setTimeout。不知道他们是如何在没有实际工作的情况下添加该功能的,但他们确实做到了。
  • Phonegap/Cordova 3 仍然存在问题。涉及编辑 AppDelegate.m 文件的解决方案是我找到的唯一解决方案。
【解决方案2】:

在 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;    
}

【讨论】:

    【解决方案3】:

    找到了解决方案 - 受到这个 PhoneGap Google group thread 的启发。据我所知,PhoneGap 在加载 webview 之前正在执行“stringByEvaluatingJavaScriptFromString”方法,因此出现了冻结问题。使用window.setTimeout函数,我们可以保证app有足够的时间加载:

    // 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;
    }
    

    【讨论】:

    • 这似乎仍然是 PhoneGap (Cordova) 2.0 中的问题。修改了 AppDelegate.m 中的上述代码,效果很好:D
    • @SomethingOn 真棒!很高兴听到它也对你有用。
    • 冷启动怎么样?这对于在后台运行的应用程序非常有用,但根据我在此处的问题stackoverflow.com/questions/12515096/… 第一次启动应用程序时不起作用
    【解决方案4】:

    我相信我的一位同事曾经提到过url包含“-”字符会导致问题,url中是否有任何“-”或“_”?

    【讨论】:

    • hmm...这是 Netflix 的 API,返回 oauth_token 和 oauth_verifier - 所以是的。 'handleOpenURL' 方法中的 url 是:myApp://?oauth_token=[VALUES]&oauth_verifier=
    • 抱歉,这不是问题所在。我尝试将 jsString 设置为“handleOpenURL("test")”(即没有破折号或下划线)但没有运气。
    • 啊。太糟糕了。远射
    猜你喜欢
    • 2014-02-23
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-30
    • 2017-06-07
    • 2014-12-19
    • 2014-08-25
    相关资源
    最近更新 更多