Native

Native ads are ad assets that are presented to users via UI components that are native to the platform. They're shown using the same classes you already use in your storyboards, and can be formatted to match your app's visual design. When a native ad loads, your app receives an ad object that contains its assets, and the app (rather than the SDK) is then responsible for displaying them. This differs from other ad formats, which don't allow you to customize the appearance of the ad.

This guide will show you how to use the Adx3 Ads SDK to implement native ads in an iOS app.

Prerequisites

Always test with test ads

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to the suspension of your account. The easiest way to load test ads is to use our dedicated test ad unit ID for all native advanced ads on iOS:

63a5739a75da4

It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

Implementation

  • Load an ad

  • Create an Adx3NativeAdView

  • Register for callbacks

  • Display the ad

Load an ad

Native ads are loaded via Adx3NativeAd objects, Which send messages to their delegates according to the Adx3NativeAdViewDelegate protocol.

The following code demonstrates how to initialize an Adx3NativeAd:

import UIKit

class ViewController: UIViewController {
    
    let nativeAd = Adx3NativeAd()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
    }
    
    func loadNativeAd() {
        nativeAd.load(withPlacementID: "Your Placement ID", userId: "", delegate: self)
    }

}

Create a Adx3NativeAdView

Before you can create Adx3NativeAdView, you need to create a Xib file, Inherited from Adx3NativeAdView. The following code demonstrates how to create an Adx3NativeAdView:

func addNativeAdView() {
    nativeAdView = Bundle.main.loadNibNamed("Adx3NativeView", owner: nil, options: nil)?.first as? Adx3NativeAdView
    nativeAdView.translatesAutoresizingMaskIntoConstraints = false
    backgroundView.addSubview(nativeAdView)
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .left, relatedBy: .equal, toItem: backgroundView, attribute: .left, multiplier: 1.0, constant: 0))
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .right, relatedBy: .equal, toItem: backgroundView, attribute: .right, multiplier: 1.0, constant: 0))
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .top, relatedBy: .equal, toItem: backgroundView, attribute: .top, multiplier: 1.0, constant: 0))
    backgroundView.addConstraint(NSLayoutConstraint.init(item: nativeAdView!, attribute: .bottom, relatedBy: .equal, toItem: backgroundView, attribute: .bottom, multiplier: 1.0, constant: 0))
}

Register for callbacks

You should rely on the Adx3RewardDelegate (optional) to handle display events and request events. The following code shows how to implement the protocol:

extension ViewController {    
    func nativeAdDidReceive(_ nativeAd: Adx3NativeAd) {
        nativeAdView.nativeAd = nativeAd;
        nativeAdView.titleLabel!.text = nativeAd.title;
        nativeAdView.subLabel!.text = nativeAd.shop_name;
        nativeAdView.mediaView!.urlString = nativeAd.material;
        nativeAdView.descLabel!.text = nativeAd.describe;
        
        let queue = DispatchQueue.global()
        queue.async { [self] in
            let data = try! Data.init(contentsOf: URL(string: nativeAd.logo)!)
            let image = UIImage(data: data)
            nativeAdView.iconImageView!.image = image
        }
    }
    
    func nativeAdDidClick(withPlacementID placementID: String, extra: [AnyHashable : Any]) {
        
    }
    
    func nativeAdDidFailToReceiveAdWithError(_ error: Error) {
        
    }
}

Display your ad

Once you have loaded an ad, all that remains is to display it to your users.

Last updated