Ad slider

Warning.

This is an archived version of the documentation. Actual documentation for all platforms can be found here.

Yandex Mobile Ads SDK lets you render a slider containing related ads. For more information about the slider, see this post.

The slider implements the native advertising paradigm. Publishers can adjust the ad layout based on its features and the design of their apps that render the slider.

  1. Loading the slider
  2. Displaying the slider

Loading the slider

  1. Create an instance of the YMANativeAdLoader class to fetch ads in the slider.

  2. Create a configuration for the nativeAdRequestConfiguration request using the YMANativeAdRequestConfiguration class. As the request parameters, you can use the ad unit ID, method for loading images, age, gender, and other data that might improve the quality of ad selection.

  3. Set a delegate for retrieving an ad that implements the YMANativeAdLoaderDelegate protocol:

  4. To track the ad loading process, implement the YMANativeAdLoaderDelegate protocol methods: -nativeAdLoader:didFailLoadingWithError: and -nativeAdLoader:didLoadAd:.

  5. To load the ad, send the loadAdWithRequestConfiguration: message to the loader.

    adLoader.loadAd(with: requestConfiguration)
  6. If the ad loaded, the following method is called:

    func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd)
  7. If the ad didn't load, the following method is called:

    func nativeAdLoader(_ loader: YMANativeAdLoader, didFailLoadingWithError error: Error)

    For more information about possible errors, see YMANativeErrorCode.

// Creating a loader 
adLoader = YMANativeAdLoader()
adLoader.delegate = self

// Creating a request configuration 
let requestConfiguration = YMANativeAdRequestConfiguration(adUnitID: "<AdUnitID>")

// Passing the request configuration to the loader
adLoader.loadAd(with: requestConfiguration)

// Implementing delegate methods
....

func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) {
    // Render the ad 
}

Displaying the slider

When the slider is loaded, you must render all its assets.

A successfully loaded slider contains one or more native ads with the same context. Ads in the slider must be displayed within the same shared container: otherwise, ad impressions won't count.

Layout without a template

When the template settings aren't enough to get the desired effect, you can configure native ads manually.

With this method, you can lay out native ads yourself by positioning ad elements in respect of each other. Your ad may contain both mandatory and optional display assets. You can find their full list in Native ad assets.

Tip.

We recommend that you use a layout that includes the complete set of possible assets. Experience has shown that layouts with a complete set of assets are more clickable.

Call the bindAdToSliderView method and pass a container for the ad slider to it.

Each ad in the slider is laid out using a standard native advertising layout method.

    func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) {
        self.ad = ad
        ad.delegate = self
        // Проверяем наличие вложенных объявлений
        if ad.ads.count != 0 {
            // Создаем контейнер для слайдера; Вместо YMANativeAdView должен быть ваш наследник этого класса
            let sliderAdView = YMANativeAdView()

            // Вызываем метод bindAd(toSliderView: _) и передаем в него контейнер
            do {
                try ad.bindAd(toSliderView: sliderAdView)
            } catch {
                // Проверяем сообщение об ошибке и исправляем проблему
                return
            }

            for subAd in ad.ads {
                // Подписываемся на делегат
                subAd.delegate = self

                // Создаем рекламное view для объявления 
                // Вместо YMANativeAdView должен быть ваш наследник этого класса
                let subAdView = YMANativeAdView()

                // Вызываем метод bind(with: subAdView) для объявления
                do {
                    try subAd.bind(with: subAdView)
                } catch {
                    // Проверяем сообщение об ошибке и исправляем проблему
                    return
                }

                // Добавляем объявление в контейнер
                sliderAdView.addSubview(subAdView)
                // Располагаем объявление в контейнере
            }

        } else {
            // Обработать как обычную нативную рекламу
            // https://yandex.ru/dev/mobile-ads/doc/ios/quick-start/config.html
        }
    }