Asynchronous Basic Integration

This placement requires iOS 13 and above.

Asynchronous native banner placements wrap the native banner placement providing the same functionality but supporting Swift Concurrency for requesting ads.

Create Placement

To create an instance of AATAsyncNativeAdPlacement, use the following API:

var placement = AATSDK.createAsyncNativeAdPlacement(name: "<PLACEMENT_NAME>", supportsMainImage: true)

The argument supportsMainImage determines if the placement will use the main image asset. Remember that if the main image is used, it must be displayed.

Listen to Callbacks

Through the use of AATNativePlacementDelegate, you can listen to the different placement callbacks.

placement.delegate = self

Request Ad

To request a native ad, use the following API:

Task {
    guard let nativeAd = await placement.reload() else {
        return
    }
    // use the nativeAd data
}

Report adspace

See the native ads ad spaces reporting.

Retrieve the Native Assets

See retrieving native ad assets documentation.

Track Native Ad

See tracking native ad data documentation.

Complete Code Example

class ViewController: UIViewController {
    // Create the placement
    var placement = AATSDK.createAsyncNativeAdPlacement(name: "<PLACEMENT_NAME>", supportsMainImage: true)
    
    // Outlets
    @IBOutlet weak var containerView: UIView!
    @IBOutlet weak var adTitleLabel: UILabel!
    @IBOutlet weak var descriptionLabel: UILabel!
    @IBOutlet weak var adIconImageView: UIImageView!
    @IBOutlet weak var adMainImageView: UIImageView!
    @IBOutlet weak var advertiserLabel: UILabel!
    @IBOutlet weak var adCTALabel: UILabel!
    @IBOutlet weak var ratingLabel: UILabel!
 
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // [IMPORTANT] Notify AATKit about the currently active view controller
        AATSDK.controllerViewDidAppear(controller: self)
        
        // Set placement delegate to listen to the callbacks
        placement.delegate = self
        
        // Request a native ad
        Task {
            guard let nativeAd = await placement.reload() else {
                return
            }
            // Assign the tracking view
            nativeAd.attachToView(containerView,
                                  mainImageView: adMainImageView,
                                  iconView: adIconImageView,
                                  ctaView: adCTALabel)
            // Report Ad Space
            placement.reportAdSpace()
            // Ad Title
            adTitleLabel.text = nativeAd.title
            // Ad Body
    	    descriptionLabel.text = nativeAd.adDescription ?? "-"
            // Ad Icon
    	    loadImage(for: adIconImageView, imageUrlString: nativeAd.iconUrl)
            // Ad Main Image
            loadImage(for: adMainImageView, imageUrlString: nativeAd.imageUrl)
            // Ad Advertiser
            advertiserLabel.text = nativeAd.advertiser ?? "-"
            // Ad CTA Title
            adCTALabel.text = nativeAd.callToAction ?? "-"
            // Ad Rating
            if let rating = nativeAd.rating {
                self.ratingLabel.text = "\(rating.value)/\(rating.scale)"
            }
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        // [IMPORTANT] Remove the currently active view controller
        AATSDK.controllerViewWillDisappear()
    }
}

// MARK: - AATNativePlacementDelegate
extension ViewController: AATAsyncNativePlacementDelegate {
    func aatPauseForAd(placement: AATPlacement) {
        // Ad has been displayed on the screen
    }

    func aatResumeAfterAd(placement: AATPlacement) {
        // Back to the app after clicking on the ad
    }
}

// MARK: - Downloading Images
extension ViewController {
    func loadImage(for imageView: UIImageView, imageUrlString: String?) {
        guard let urlString = imageUrlString,
              let url = URL(string: urlString) else {
            return
        }
        DispatchQueue.global().async {
            guard let data = try? Data(contentsOf: url),
                let image = UIImage(data: data) else {
                return
            }
            DispatchQueue.main.async {
                imageView.image = image
            }
        }
    }
}

Last updated