PhoneGap / Cordova打开外部链接到Safari,而不是全屏InApp [英] PhoneGap/Cordova open external link into Safari instead of full screen inApp

查看:1566
本文介绍了PhoneGap / Cordova打开外部链接到Safari,而不是全屏InApp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程式中嵌入了Google地图。
在嵌入式地图(这是一个iFrame)中,我有来自google的链接:使用条款
当我点击链接(客户可以做错误或不),它打开我的应用程序和全屏的页面。不能再回到应用程序!

I am embedding a GoogleMap in my app. On the embedded map (which is an iFrame) I have links from google:'term of use'. When I click on the link (a customer could do that by mistake or not) it opens the page in my app and full screen. Impossible then to get back to the app!

所以我添加了Cordova插件 InAppBrowser 。我创建了一个脚本以防止:

So I added the Cordova Plugin InAppBrowser. And I created a little script to prevent that:

 $("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");
        }
    });

它在页面上的经典链接上工作得很好。
主要的问题是google地图是在iFrame!这样我无法从jQuery和$(a)访问元素。click(.....)不工作的iFrame链接!

And it's working very well on a classic link on the page. The main issue is that google Map is in an iFrame! This way I can't access to elements from jQuery and the $("a').click(.....) is not working on the iFrame Link !

总结

我想要在Safari中打开任何外部链接(http,wwww,.fr,.com,.org ...等)而不是我的应用程序,或使用InAppBrowser插件,在我的应用程序中打开但不是全屏,返回应用程序。

推荐答案

我找到了一个解决方案,这可能不是最好的一个,因为它只为iOs(当然可以扩展到android):

I have found a solution, this is maybe not the best one as it's only for iOs (certainly scalable to android):

我在MainViewController.m中添加了这个代码:

I added this code in the 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网址为: https://www.google.com/maps/embed/v1/directions?key=MyAPI_Key&origin=Campbell&destination=Sunnyvale

This way any http or https will be forwarded to Safari.app. Pay attention because the iFrame is understood as an external link (as it is XSS iFrame) and so it was opened in Safari, so I did the fix by adding the check mustNotReroot. The iFrame URL is : https://www.google.com/maps/embed/v1/directions?key=MyAPI_Key&origin=Campbell&destination=Sunnyvale

因为它包含嵌入我检查它是否包含嵌入,如果是:不转发到Safari。

As it contains embed I check if it contains embed, if yes: do not forward to Safari.

如果你有一个更好的修复,免费分享!

If you have a better fix like a javascript fix working on both iOS and Android feel free to share it !

这篇关于PhoneGap / Cordova打开外部链接到Safari,而不是全屏InApp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆