Admob Native Advanced不可点击

时间:2018-01-29 11:00:07

标签: ios objective-c xcode admob google-admob

我创建了一个继承自 GADNativeContentAdView 类的自定义View类。当我收到广告并且调用了委托时,我将自定义视图填入如下所示的数据。

一切看起来都不错,但问题在于它根本无法点击。我试图将actionbutton userinteraction设置为false,但仍然不起作用。 我还尝试使用以下注册:

  

- (void)registerAdView:(UIView *)adView          clickableAssetViews:(NSDictionary *)clickableAssetViews       nonclickableAssetViews:           (NSDictionary *)nonclickableAssetViews;

知道如何让它发挥作用吗?

- (void)setNativeContent:(GADNativeContentAd *)nativeContent
{
    self.nativeContentAd = nativeContent;
    headlineLabel.text = nativeContent.headline;
    bodyLabel.text = nativeContent.body;
    advertiserImage.image = ((GADNativeAdImage *)nativeContent.images.firstObject).image;
    [actionButton setTitle:nativeContent.callToAction forState:UIControlStateNormal];
    if (nativeContent.logo && nativeContent.logo.image)
    {
        advertiserLogo.image = nativeContent.logo.image;
    }
    else
    {
        advertiserLogo.image = advertiserImage.image;
    }
    NSDictionary *clickableArea = @{GADNativeContentHeadlineAsset:headlineLabel, GADNativeContentImageAsset:advertiserImage, GADNativeContentCallToActionAsset:actionButton};

    NSDictionary *nonClickableArea = @{GADNativeContentBodyAsset:bodyLabel};

    [nativeContent registerAdView:self clickableAssetViews:clickableArea nonclickableAssetViews:nonClickableArea];
}

4 个答案:

答案 0 :(得分:1)

只有AdMob列入白名单的发布商可以使用registerAdView API:)

所有发布者都可以使用xib创建广告视图。

答案 1 :(得分:0)

在方法内部,您只需在视图层次结构中创建和放置广告。

 GADNativeContentAdView *contentAdView = [[NSBundle mainBundle] loadNibNamed:@"NativeAdView" owner:nil options:nil].firstObject;

分配属性后,将内容广告视图与内容广告对象相关联。这是广告可点击所必需的。

contentAdView.nativeContentAd = nativeContentAd;

答案 2 :(得分:0)

我终于找到了一种方法,可以在不使用.xib的情况下点击整个原生广告。我将GADNativeContentAdView子类化,并创建了一个tappableOverlay视图,我将其分配给其超类中未使用的资产视图。在这种情况下,它是callToActionView。然后我使用了未记录的GADNativeContentAd.registerAdView()方法:

- (void)registerAdView:(UIView *)adView
   clickableAssetViews:(NSDictionary<GADNativeContentAdAssetID, UIView *> *)clickableAssetViews
   nonclickableAssetViews: (NSDictionary<GADNativeContentAdAssetID, UIView *> *)nonclickableAssetViews;

这是一个Swift 4示例:

class NativeContentAdView: GADNativeContentAdView  {
    var nativeAdAssets: NativeAdAssets?

    private let myImageView: UIImageView = {
        let myImageView = UIImageView()
        myImageView.translatesAutoresizingMaskIntoConstraints = false
        myImageView.contentMode = .scaleAspectFill
        myImageView.clipsToBounds = true
        return myImageView
    }()

    private let myHeadlineView: UILabel = {
        let myHeadlineView = UILabel()
        myHeadlineView.translatesAutoresizingMaskIntoConstraints = false
        myHeadlineView.numberOfLines = 0
        myHeadlineView.textColor = .black
        return myHeadlineView
    }()

    private let tappableOverlay: UIView = {
        let tappableOverlay = UIView()
        tappableOverlay.translatesAutoresizingMaskIntoConstraints = false
        tappableOverlay.isUserInteractionEnabled = true
        return tappableOverlay
    }()

    private let adAttribution: UILabel = {
        let adAttribution = UILabel()
        adAttribution.translatesAutoresizingMaskIntoConstraints = false
        adAttribution.text = "Ad"
        adAttribution.textColor = .white
        adAttribution.textAlignment = .center
        adAttribution.backgroundColor = UIColor(red: 1, green: 0.8, blue: 0.4, alpha: 1)
        adAttribution.font = UIFont.systemFont(ofSize: 11, weight: UIFont.Weight.semibold)
        return adAttribution
    }()

    override var nativeContentAd: GADNativeContentAd? {
        didSet {
            if let nativeContentAd = nativeContentAd, let callToActionView = callToActionView {
                nativeContentAd.register(self,
                                         clickableAssetViews: [GADNativeContentAdAssetID.callToActionAsset: callToActionView],
                                         nonclickableAssetViews: [:])
            }
        }
    }

    init() {
        super.init(frame: CGRect.zero)

        translatesAutoresizingMaskIntoConstraints = false
        backgroundColor = .white
        isUserInteractionEnabled = true
        callToActionView = tappableOverlay
        headlineView = myHeadlineView
        imageView = myImageView
    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func didMoveToSuperview() {
        super.didMoveToSuperview()

        addSubview(myHeadlineView)
        addSubview(myImageView)
        addSubview(adAttribution)
        addSubview(tappableOverlay)
    }

//    override func updateConstraints() {
//          ....
//    }
}

请确保将tappableOverlay固定到其超视图边缘,以便它们在updateConstraints()中具有相同的尺寸....

答案 3 :(得分:0)

不要忘记将 custom GADUnifiedNativeAdView商店链接到您的UILabel,UIButton和ImageView,因此GADUnifiedNativeAdView将知道要与之交互的内容 enter image description here

These are custom outlets, drag them directly to your storyboard elements