Call to action not triggered in Native google ads implementation

596 Views Asked by At

I am trying to implement the native ads following this guide Google Native ad UI guide

The Ui I implemented as test is as such: enter image description here

In this ui the CGADNativeAdView class is not available by default so I have followed the solution provided in this link

I have connected the outlets of the views as such Outlet connection in view

Here the UIButton is responsible for triggering the CallToAction event of nativeAd. But the action is not triggered when pressing the button.

Also here is a view I am creating out of this xib file.

import UIKit

import GoogleMobileAds import Cosmos

class TestBannerView: GADNativeAdView { var containerView: UIView!

override init(frame: CGRect) {
    super.init(frame: frame)
    commonInit()
}
required init?(coder: NSCoder) {
    super.init(coder: coder)
    commonInit()
}
//
private func commonInit() {
    let bundle = Bundle(for: TestBannerView.self)
    containerView = UINib(nibName: "WhiteFullBannerAdView", bundle: bundle).instantiate(withOwner: self).first as? UIView
    containerView!.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    containerView!.frame = bounds
    addSubview(containerView!)
}

}
extension TestBannerView: SetUpNativeAdViewDelegate {
func setUpAd(_ ad: GADNativeAd) {
    guard let nativeAdView = containerView as? GADNativeAdView else {
        return
    }
    if let headerText = nativeAdView.headlineView as? UILabel, let header = ad.headline {
        headerText.text = header
    }
    if let iconView = nativeAdView.iconView as? UIImageView, let icon = ad.icon?.image {
        iconView.image = icon
    }
    if let callToActionView = nativeAdView.callToActionView as? UIButton {
        callToActionView.setTitle(ad.callToAction, for: .normal)
        callToActionView.isHidden = ad.callToAction == nil
    }
    
//         In order for the SDK to process touch events properly, user interaction should be disabled.
    nativeAdView.callToActionView?.isUserInteractionEnabled = false
    // Associate the native ad view with the native ad object. This is
    // required to make the ad clickable.
    // Note: this should always be done after populating the ad views.
    nativeAdView.nativeAd = nativeAd
    self.nativeAd?.delegate = self
    
}

@objc func clickyclicky() {
    print("clickyclicky")
}
}
extension TestBannerView: GADNativeAdDelegate {
func nativeAdDidRecordClick(_ nativeAd: GADNativeAd) {
    print("native ad recorded click")
}

func nativeAdDidRecordImpression(_ nativeAd: GADNativeAd) {
    print("native ad recorded impression")
}

func nativeAdWillPresentScreen(_ nativeAd: GADNativeAd) {
    print("native ad will present screen")
}
}

With this I am able to load the TestBannerView in the app. It would be a great help if someone can point out why calltoaction is not triggered.

0

There are 0 best solutions below