Display native ad on Apple

Step 1: Layout the native ad in a storyboard or nib file.

The types of views that can be configured in a native ad are as follows:

  • Icon

  • Advertiser

  • Headline

  • Body

  • Media

  • Call to action

For example, when configuring a social feed ad, you can lay it out as follows:

  • AdContainerView - superview (AdropNativeAdView)

    ⌞ Icon (UIImageView)

    ⌞ Advertiser (UILabel)

    ⌞ Media (UIView)

    ⌞ Body (UILabel)

Please make sure the superview is declared as AdropNativeAdView. You can modify this in the Custom Class section of the Identity Inspector.

import UIKit
import AdropAds

class FeedContentTableViewCell: UITableViewCell {
    @IBOutlet weak var adContainerView: AdropNativeAdView!
    
    @IBOutlet weak var iconImageView: UIImageView!
    @IBOutlet weak var nameLabel: UILabel!
    @IBOutlet weak var mediaView: UIView!
    @IBOutlet weak var bodyLabel: UILabel!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        reset()
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        reset()
    }
    
    private func reset() {
        iconImageView.image = nil
        nameLabel.text = ""
        bodyLabel.text = ""
    }
}

Step 2: Load a native ad

class NativeAdViewController: UIViewController {
    private var ad: AdropNativeAd!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        ad = AdropNativeAd(unitId: "PUBLIC_TEST_UNIT_ID_NATIVE")
        ad.delegate = self
        ad.load()
    }
}

extension NativeAdViewController: AdropNativeAdDelegate {
    func onAdReceived(_ ad: AdropAds.AdropNativeAd) {
        self.ad = ad
        print("onAdReceived")
    }
    
    func onAdFailedToReceive(_ ad: AdropAds.AdropNativeAd, _ errorCode: AdropAds.AdropErrorCode) {
        print("onAdFailedToReceive")
    }
    
    func onAdClicked(_ ad: AdropAds.AdropNativeAd) {
        print("onAdClicked")
    }
}

Step 3: Connect a native ad to view

Here is an example that connects the subviews of AdropNativeAdView and then populates the AdropNativeAd.

extension NativeAdFeedViewController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FeedAdTableViewCell", for: indexPath) as! FeedContentTableViewCell
        cell.adContainerView.setIconView(cell.iconImageView)
        cell.adContainerView.setAdvertiserView(cell.nameLabel)
        cell.adContainerView.setMediaView(cell.mediaView)
        cell.adContainerView.setBodyView(cell.bodyLabel)
        cell.adContainerView.setNativeAd(ad)
        
        return cell
    }
}

Last updated