0

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];
}
Ali
  • 497
  • 1
  • 4
  • 16
  • registerAdView is a new, experimental feature we're testing with just a few publishers. If you haven't already been talking with AdMob as part of a beta program, it's likely that you wouldn't be able to use it. If that's the case, the SDK should be printing something to your log. – RedBrogdon Jan 31 '18 at 16:53

5 Answers5

4

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().

mph
  • 808
  • 1
  • 10
  • 22
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;
Vinu David Jose
  • 2,569
  • 1
  • 21
  • 38
  • I do not want to use xib file, I want to create it programmatically. I have the property set "contentAdView.nativeContentAd = nativeContentAd" – Ali Jan 29 '18 at 13:24
  • hi zahr,i itself want to load ad without using xib file,did you found any solution regarding this – Gowtham Apr 02 '18 at 04:22
1

Only AdMob whitelisted publishers can use the registerAdView API :)

All publishers can use xib to create an ad view.

Jill Song
  • 11
  • 1
0

Don't forget to link custom GADUnifiedNativeAdView outlets to your UILabels, UIButtons and ImageViews, so GADUnifiedNativeAdView will know what to interact with enter image description here

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

Alex Metelkin
  • 1,520
  • 7
  • 13
0

In my case it was cause I created my views without xib. In this case just set mediaView property to your GADNativeAdView

here the minimum working code

final class EndBannerController: UIViewController {
        
    private let adId: String
    private let adView = GADNativeAdView()
    private let mediaView = GADMediaView()
    
    private var adLoader: GADAdLoader?
    
    init(adId: String) {
        self.adId = adId
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) { return nil }

    override func viewDidLoad() {
        super.viewDidLoad()

        adView.frame = view.bounds
        view.addSubview(adView)
        
        mediaView.frame = view.bounds
        adView.mediaView = mediaView
        adView.addSubview(mediaView)
        
        let loader = GADAdLoader(
            adUnitID: adId,
            rootViewController: self,
            adTypes: [.native],
            options: nil
        )

        loader.delegate = self
        
        self.adLoader = loader
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.loadBannerAd()
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        adView.frame = view.bounds
        mediaView.frame = view.bounds
    }
    
    private func loadBannerAd() {
        let request = GADRequest()
        request.scene = view.window?.windowScene
        self.adLoader?.load(request)
    }
}