7

好的,浏览问题我找到了将外部页面加载到 phonegap 视图中的正确方法(即不丢失会话或打开设备浏览器),如下所述:如何在 phonegap webview 中加载网页? 在这里:iPhone 的 PhoneGap:加载外部 URL 时出现问题

下一步是:在我打开一个外部页面(它归我所有,我可以修改它)之后,我怎样才能回到我的本地应用程序?假设我在外部页面中有一个链接,我希望用户在点击时被重定向回 phonegap 应用程序内的本地 html 页面 (mypage.html)。

链接的 href 属性应该有什么 url?我尝试将其设置为“file:///android_asset/www/mypage.html”但没有用

4

3 回答 3

3

您想使用ChildBrowser插件打开外部网页。然后您想将 ChildBrowser.onLocationChange 属性设置为您自己的函数。然后,当此人离开远程页面时,您将收到位置更改通知,以便您可以关闭 ChildBrowser 并导航到新的本地页面。您甚至不需要触摸远程 html 页面。

因此,当用户离开远程页面时关闭浏览器:

cb.onLocationChange = function(loc){
    console.log("location = " + loc);
    if (loc != "http://example.com") {
        cb.close();
    }
}; 
于 2012-10-17T20:08:29.780 回答
1

您需要的是 MainViewController.m 中的这个迷人者 它在科尔多瓦 1.7.0 科尔多瓦 1.9.0 和科尔多瓦 2.1.0 中对我有用

- (BOOL)webView:(UIWebView *)theWebView shouldStartLoadWithRequest:(NSURLRequest *)request         navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];

// Intercept the external http requests and forward to Safari.app
// Otherwise forward to the PhoneGap WebView
if ([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"]) {
[[UIApplication sharedApplication] openURL:url];
return NO;
}
else {
return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ];
}
    }
于 2012-11-05T09:12:27.477 回答
1

这是使用 PhoneGap/Cordova 2.7。在您的外部应用程序中,添加一个指向“app://index”的链接。

在 onCreate 里面添加:

this.appView.setWebViewClient(new CordovaWebViewClient(this, this.appView) {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.equalsIgnoreCase("app://index")) {
            Log.d("DEBUG", url);

            loadUrl(Config.getStartUrl());

            return true;
        } else {
            return super.shouldOverrideUrlLoading(view, url);
        }
    }
});

这将拦截呼叫并将用户重定向到配置的起始 url。

于 2013-06-06T15:19:35.850 回答