> For the complete documentation index, see [llms.txt](https://ios-docs.adster.tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ios-docs.adster.tech/multiple-ads/carousel-native-ad.md).

# Carousel Native Ad

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

**Implementation:**

```swift
extension NativeAdManager: MediationAdDelegate {
    func onCarouselNativeAdLoaded(carouselNativeAd: AdsFramework.MediationCarouselNativeAd) {
        guard !carouselNativeAd.ads.isEmpty else {
            print("Carousel Native Ad request failed with reason no native ads")
            return
        }

        addCarouselNativeAdsToView(carouselNativeAd.ads)
    }

    func onAdFailedToLoad(error: AdError) {
        print("Native Ad request failed with reason \(error.description)")
    }
    
    func onAdRevenuePaid(revenue: Double, adUnitId: String, network: String, currency: String, precisionType: AdsFramework.PrecisionType) {
        print("Revenue: \(revenue) \(currency), adUnitId: \(adUnitId), network: \(network), precision: \(precisionType)")
        
    }
}
```

**Example carousel rendering:**

```swift
private func addCarouselNativeAdsToView(_ nativeAds: [AdsFramework.MediationNativeAd]) {
    carouselStackView.arrangedSubviews.forEach { view in
        carouselStackView.removeArrangedSubview(view)
        view.removeFromSuperview()
    }

    for nativeAd in nativeAds {
        nativeAd.eventDelegate = self

        guard let nibObjects = Bundle.main.loadNibNamed("NativeView", owner: nil, options: nil),
              let adView = nibObjects.first as? MediationNativeAdView else {
            print("Could not load nib file for adView")
            continue
        }

        populateNativeAdView(adView, with: nativeAd)

        let slideView = UIView()
        slideView.translatesAutoresizingMaskIntoConstraints = false

        adView.translatesAutoresizingMaskIntoConstraints = false
        slideView.addSubview(adView)

        NSLayoutConstraint.activate([
            adView.leadingAnchor.constraint(equalTo: slideView.leadingAnchor),
            adView.trailingAnchor.constraint(equalTo: slideView.trailingAnchor),
            adView.topAnchor.constraint(equalTo: slideView.topAnchor),
            adView.bottomAnchor.constraint(equalTo: slideView.bottomAnchor),

            slideView.widthAnchor.constraint(equalTo: carouselScrollView.frameLayoutGuide.widthAnchor),
            slideView.heightAnchor.constraint(equalTo: carouselScrollView.frameLayoutGuide.heightAnchor)
        ])

        carouselStackView.addArrangedSubview(slideView)
    }

    pageControl.numberOfPages = nativeAds.count
    pageControl.currentPage = 0
}
```

**Helper method:**

```swift
private func populateNativeAdView(
    _ adView: MediationNativeAdView,
    with nativeAd: AdsFramework.MediationNativeAd
) {
    (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)
}
```

**Tracking Events:**

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

```swift
extension NativeAdManager: MediationNativeAdEventDelegate {
    func recordNativeClick() {
        print("Native ad clicked")
    }
    
    func recordNativeImpression() {
        print("Native ad impression recorded")
    }
}
```

{% hint style="info" %}
Carousel Native Ads return multiple native ads in `carouselNativeAd.ads`. \
Each item is a `MediationNativeAd`, so create one `MediationNativeAdView` for each ad, populate it with the ad data, call `setNativeAd(nativeAd:)`, and assign `eventDelegate` for click and impression tracking.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://ios-docs.adster.tech/multiple-ads/carousel-native-ad.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
