Admob Native Advanced Not Clickable

2019-08-25 20:07发布

问题:

I have created a custom View Class that inherits from GADNativeContentAdView Class. When I receive an advertisement and the delegate is called, I fill my custom view with the data as shown below.

Everything looks fine but the problem is that it is not clickable at all. I tried to set the actionbutton userinteraction to false, but still won't work. I also tried to register using following:

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

Any idea how to get it to work?

- (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];
}

回答1:

Inside the method simply you can create and place Ad in view hierarchy.

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

After assigning the properties, associate the content Ad view with the content ad object. This is required to make the ad clickable.

contentAdView.nativeContentAd = nativeContentAd;


回答2:

I finally figured out a way to make the entire native ad clickable without using a .xib. I subclassed GADNativeContentAdView and created a tappableOverlay view that I assigned to an unused asset view in its superclass. In this case, it was the callToActionView. Then I used the not-so-documented GADNativeContentAd.registerAdView() method:

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

Here's a Swift 4 example:

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() {
//          ....
//    }
}

Just be sure to pin the tappableOverlay to its superview edges so that they're the same size...in updateConstraints().