2014-04-29 40 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修复程序一样,随时分享吧!

相关问题