🖥️Native Ad

To receive Native Ads, conform to the MediationAdDelegate protocol. Once the ad is loaded, you can populate your custom native ad view with the ad's data.

Implementation:

extension MainViewController: MediationAdDelegate {
    func onNativeAdLoaded(nativeAd: any MediationNativeAd) {
        nativeAd.eventDelegate = self
        guard let nibObjects = Bundle.main.loadNibNamed("NativeView", owner: nil, options: nil),
              let adView = nibObjects.first as? MediationNativeAdView else {
            printLog("Could not load nib file for adView")
            return
        }
        (adView.bodyView as? UILabel)?.text = nativeAd.body
        (adView.headlineView as? UILabel)?.text = nativeAd.headline
        (adView.ctaView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
        (adView.ctaView as? UIButton)?.isUserInteractionEnabled = false
        if let mediaView = nativeAd.mediaView {
            addMediaViewToParentView(childView: mediaView, parentView: adView.mediaView)
        }
        
        adView.setNativeAd(nativeAd: nativeAd)
        viewBanner.addSubview(adView)
    }

    func onAdFailedToLoad(error: AdError) {
        print("Native Ad request failed with reason \(error.description)")
    }
}

Tracking Events:

To track impression and click events for Native Ads, conform to MediationNativeAdEventDelegate:

extension MainViewController: MediationNativeAdEventDelegate {
    func recordClick() {
        // Ad click recorded
    }
    
    func recordImpression() {
        // Ad impression recorded
    }
}

Last updated