PhoneGap:如何在InAppBrowser中打开iframe的链接

big*_*801 6 jquery android ios phonegap-plugins cordova

我使用谷歌dfp在我的PhoneGap项目中投放广告,这些广告呈现为iframe.

当用户点击广告时,我希望在网址中打开该网址InAppBrowser.截至目前,它只是打开了网址WebView.

iframe中的锚标签有target="_blank",但我相信因为它在iframe中,PhoneGap忽略了这一点.

我知道我InAppBrowser正在为我项目中的其他链接工作,所以我已经排除了这一点.

以下是我的config.xml中的一些设置:

...

<feature name="InAppBrowser">
    <param name="ios-package" value="CDVInAppBrowser" />
</feature>
<feature name="InAppBrowser">
    <param name="android-package" value="org.apache.cordova.InAppBrowser" />
</feature>

...

<preference name="stay-in-webview" value="false" />

...

<access origin="*" />
Run Code Online (Sandbox Code Playgroud)

这就是呈现的iframe的样子:

<div class="adunit display-block" data-adunit="example_app_section_front_footer" data-dimensions="320x50" id="example_app_section_front_footer-auto-gen-id-1">
    <div id="google_ads_iframe_/2444258/example_app_section_front_footer_0__container__" style="border: 0pt none;">
        <iframe id="google_ads_iframe_/2444258/example_app_section_front_footer_0" name="google_ads_iframe_/2444258/example_app_section_front_footer_0" width="320" height="50" scrolling="no" marginwidth="0" marginheight="0" frameborder="0" src="someurl"  style="border: 0px; vertical-align: bottom;">
            <html>
                <body>
                    <div id="google_image_div">
                        <a id="aw0" target="_blank" href="http://googleads.g.doubleclick.net/aclk?someurl" onfocus="ss('aw0')" onmousedown="st('aw0')" onmouseover="ss('aw0')" onclick="ha('aw0')"><img src="http://pagead2.googlesyndication.com/simgad/000111222" border="0" width="320" height="50" alt="" class="img_ad"></a>
                    </div>
                </body>
            </html>        
        </iframe>
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

我目前有一些jQuery检查所有具有目标的锚标签_blank并在其中打开它们InAppBrowser,但是此功能不适用于渲染的iframe:

$(document).on('click', 'a[target=_blank]', function (event) {
    event.preventDefault();
    window.open($(this).attr('href'), '_blank');
});
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

Riv*_*era 3

由于这些是常规链接而不是cordova_exec调用,因此您需要按照上面的建议进行子类化CDVViewController和实现webView:shouldStartLoadWithRequest:navigationType

确保调用super您的实现。


要子类化,CDVViewController您必须编写一些本机代码。

至少是这样的:

// MyGapViewController.h

@interface MyGapViewController : CDVViewController

@end



// MyGapViewController.h

@implementation MyGapViewController

- (BOOL)webView:(UIWebView*)webView
shouldStartLoadWithRequest:(NSURLRequest*)request
 navigationType:(UIWebViewNavigationType)navigationType
{
    // Check if super says we should load it
    BOOL shouldLoad = [super webView:webView
          shouldStartLoadWithRequest:request
                      navigationType:navigationType];

    // If super says NO, then no need to continue
    if (!shouldLoad)
    {
        return NO;
    }

    // Else check here if it is one of our iframe links and do something about it...
    if (iFrame)
    {
        //...
        return NO; // We handled it no need to load the iframe link in the original web view
    }

    // Else load it here
    return YES;
}

@end
Run Code Online (Sandbox Code Playgroud)