2012-10-17 47 views
7

好的,瀏覽問題我找到了正確的方法來將外部頁面加載到phonegap視圖中(即不會丟失會話或打開設備瀏覽器),如此處所述:How can I load a webpage inside the phonegap webview? 這裏:PhoneGap for iPhone: problem loading external URLphonegap:打開外部頁面,然後返回到應用程序

下一步是:我已經打開的頁面extarnal後(它是由我擁有,我可以修改它)我怎麼能回到我的本地應用程序?比方說,我在外部頁面中有一個鏈接,並且我希望用戶在點擊時在phonegap應用程序內被重定向回本地html頁面(mypage.html)。

該鏈接的href屬性應具有哪些url?我嘗試將其設置爲「file:///android_asset/www/mypage.html」,但未起作用

回答

3

您想使用ChildBrowser插件打開外部網頁。然後,您要將ChildBrowser.onLocationChange屬性設置爲您自己的功能。然後,當人離開遠程頁面時,您會收到位置更改的通知,因此您可以關閉ChildBrowser並導航到新的本地頁面。您甚至不需要觸摸遠程HTML頁面。

因此,當用戶從遠程頁面導航離開關閉瀏覽器:

cb.onLocationChange = function(loc){ 
    console.log("location = " + loc); 
    if (loc != "http://example.com") { 
     cb.close(); 
    } 
}; 
+0

非常感謝。順便說一句,因爲這只是我解決另一個問題,有沒有辦法從外部頁面回到本地頁面,我不能編輯,但可以接收回調url作爲參數? –

+0

是的,你可以做到。我只是給我的答案添加了一些代碼。 –

+0

太好了,謝謝! –

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 ]; 
} 
    } 
+0

謝謝。這似乎很漂亮的iPhone相關,我會檢查它在科多諾安卓也。 –

+0

是的,它是iPhone相關的。你不應該有任何與cordova-android有關的問題 – iOSAndroidWindowsMobileAppsDev

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。

相關問題