2014-04-29 42 views
0

我在我的應用程序中嵌入GoogleMap。 在嵌入式地圖(這是一個iFrame)我有谷歌鏈接:'term of use'。 當我點擊鏈接(客戶可能會錯誤地做到這一點),它會在我的應用程序和全屏幕中打開頁面。不可能然後回到應用程序!PhoneGap /科爾多瓦開放外部鏈接到Safari而不是全屏inApp

所以我添加了Cordova Plugin InAppBrowser。我創建了一個小腳本來防止這種情況發生:

$("a").click(function(e) { 
     console.log("checking link"); 
     var link = e.currentTarget.href; 
     var testURL = (link.indexOf("www") > (-1)); 
     testURL |= (link.indexOf("http") > (-1)); 
     testURL |= (link.indexOf(".com") > (-1)); 
     testURL |= (link.indexOf(".fr") > (-1)); 

     if (testURL) {/*if URL is external open in 'new window'*/ 
      console.log("Prevent InApp URL FullScreen"); 
      window.open(link, "_system"); 
     } 
    }); 

而且它在頁面上的經典鏈接上運行得非常好。 主要問題是谷歌地圖是在一個iFrame!這樣,我無法訪問從jQuery的元素與$(「A」)。單擊(.....)不工作的iFrame的鏈接!

,總結
我想任何外部鏈接(http,wwww,.fr,.com,.org等)在Safari中打開而不是我的應用程序或使用InAppBrowser插件在我的應用程序中打開但不是全屏,到App。

你有什麼想法?
在此先感謝球員。

回答

4

我已經找到了解決方案,這是也許不是TH Ë最好的一個,因爲它是隻適用於iOS(當然可擴展到Android):

我加入這個代碼在MainViewController.m:

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

    //mustNotReroot : for the google iFrame, the adress contains "embed" ==> must not be open in safari but in the app 
    BOOL mustNotReroot = [url.path rangeOfString:@"embed"].location != NSNotFound; 

    // Intercept any external http requests and forward to Safari 
    // Otherwise forward to the PhoneGap WebView 

    if (([[url scheme] isEqualToString:@"http"] || [[url scheme] isEqualToString:@"https"])&& !mustNotReroot) 
    { 
     NSLog(@"Rerouting to Safari %@",url); 
     [[UIApplication sharedApplication] openURL:url]; 
     return NO; 
    } 
    else 
    { 
     return [ super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType ]; 
    } 
} 

這樣任何HTTP或HTTPS將被轉發到Safari.app。請注意,因爲iFrame被理解爲外部鏈接(因爲它是XSS iFrame),所以它在Safari中打開,所以我通過添加檢查mustNotReroot來完成修復。 該iFrame的URL是:https://www.google.com/maps/embed/v1/directions?key=MyAPI_Key&origin=Campbell&destination=Sunnyvale

因爲它包含嵌入我檢查它是否包含嵌入,如果是的話:不要轉發到Safari。

如果你有更好的解決方案,就像在iOS和Android上工作的JavaScript修復程序一樣,隨時分享吧!

相關問題