我正在使用google 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="*" />
这就是呈现的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>
我目前有一些jQuery会检查目标为_blank
的所有锚标记并在InAppBrowser
中打开它们,但是此功能不适用于呈现的iframe:
$(document).on('click', 'a[target=_blank]', function (event) {
event.preventDefault();
window.open($(this).attr('href'), '_blank');
});
任何帮助都将不胜感激。
答案 0 :(得分: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