Ad slider
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.
Loading the slider
Create an instance of the YMANativeAdLoader class to fetch ads in the slider.
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.Set a delegate for retrieving an ad that implements the YMANativeAdLoaderDelegate protocol:
To track the ad loading process, implement the YMANativeAdLoaderDelegate protocol methods: -nativeAdLoader:didFailLoadingWithError: and -nativeAdLoader:didLoadAd:.
To load the ad, send the
loadAdWithRequestConfiguration:
message to the loader.adLoader.loadAd(with: requestConfiguration)
[self.adLoader loadAdWithRequestConfiguration:requestConfiguration];
If the ad loaded, the following method is called:
func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd)
- (void)nativeAdLoader:(YMANativeAdLoader *)loader didLoadAd:(id<YMANativeAd>)ad
If the ad didn't load, the following method is called:
func nativeAdLoader(_ loader: YMANativeAdLoader, didFailLoadingWithError error: Error)
- (void)nativeAdLoader:(YMANativeAdLoader *)loader didFailLoadingWithError:(NSError *)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
}
// Creating a loader self.adLoader = [[YMANativeAdLoader alloc] init];
self.adLoader.delegate = self;
// Creating a request configuration
YMANativeAdRequestConfiguration *requestConfiguration =
[[YMANativeAdRequestConfiguration alloc] initWithAdUnitID:@"your_AdUnitID"];
// Passing the request configuration to the loader
[self.adLoader loadAdWithRequestConfiguration:requestConfiguration];
// Implementing delegate methods
....
- (void)nativeAdLoader:(YMANativeAdLoader *)loader didLoadAd:(id<YMANativeAd>)ad
{
// 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.
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
}
}
- (void)nativeAdLoader:(YMANativeAdLoader *)loader didLoadAd:(id<YMANativeAd>)ad
{
self.ad = ad;
self.ad.delegate = self;
// Проверяем наличие вложенных объявлений
if (ad.ads.count != 0) {
// Создаем контейнер для слайдера
YMANativeAdView *sliderAdView = [[YMANativeAdView alloc] init];
// Вызываем метод bindAdToSliderView и передаем в него контейнер
NSError *error = nil;
[ad bindAdToSliderView:sliderAdView error:&error];
// Проверяем успешность привязки
if (error != nil) {
// Проверяем сообщение об ошибке и исправляем проблему
return;
}
for (id<YMANativeAd> subAd in ad.ads) {
// Подписываемся на делегат
subAd.delegate = self;
// Создаем рекламное view для объявления
// Вместо YMANativeAdView должен быть ваш наследник этого класса
YMANativeAdView *subAdView = [[YMANativeAdView alloc] init];
NSError *error = nil;
// Вызываем метод bindWithAdView и передаем в него рекламное view
[subAd bindWithAdView:subAdView error:&error];
// Проверяем успешность привязки
if (error != nil) {
// Проверяем сообщение об ошибке и исправляем проблему
continue;
}
// Добавляем объявление в контейнер
[sliderAdView addSubview:subAdView];
// Располагаем объявление в контейнере
}
} else {
// Обработать как обычную нативную рекламу
// https://yandex.ru/dev/mobile-ads/doc/ios/quick-start/config.html
}
}