【问题标题】:PhoneGap/Cordova open external link into Safari instead of full screen inAppPhoneGap/Cordova 打开外部链接到 Safari 而不是全屏 inApp
【发布时间】:2014-04-29 15:43:19
【问题描述】:

我在我的应用程序中嵌入了 GoogleMap。 在嵌入式地图(这是一个 iFrame)上,我有来自 google:'term of use' 的链接。 当我单击链接时(客户可能会错误地这样做),它会在我的应用程序中打开页面并全屏显示。那么不可能回到应用程序!

所以我添加了 Cordova 插件 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').click(.....) 在 iF​​rame Link 上不起作用!

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

你有什么想法吗?
在此先感谢各位。

【问题讨论】:

    标签: javascript html iframe cordova


    【解决方案1】:

    我找到了一个解决方案,这可能不是最好的,因为它只适用于 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 修复程序,请随时分享!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 1970-01-01
      相关资源
      最近更新 更多