Ad slider
Native advertising is an ad type where the layout can be defined on the app side. This feature allows you to change the visual style of ads and their placement, considering the app design specifics.
Ad rendering is performed with native platform tools, which enhances ad performance and quality.
Native ads enhance the ad experience. As a result, you can display more ads without losing user interest. This ensures maximum revenue from advertising in the long run.
Prerequisite
- Follow the SDK integration steps described in Quick start.
- Initialize your ad SDK in advance.
- Make sure you're running the latest Yandex Mobile Ads SDK version. If you're using mediation, make sure you're also running the latest version of the unified build.
Loading the slider
-
Create an instance of the
SliderAdLoaderclass to load the ad slider. -
Create an
AdRequestwith the ad unit ID andNativeAdOptionsfor additional parameters. -
Call
loadAd(with:options:completion:)to load the ad. -
If the ad loads successfully, use the
SliderAdobject to display it. -
If loading fails, handle the error.
let adLoader = SliderAdLoader()
let request = AdRequest(adUnitID: "<AdUnitID>")
let options = NativeAdOptions()
do {
let sliderAd = try await adLoader.loadAd(with: request, options: options)
// Display the ad
} catch {
// Load error
}
let adLoader = SliderAdLoader()
let request = AdRequest(adUnitID: "<AdUnitID>")
let options = NativeAdOptions()
adLoader.loadAd(with: request, options: options) { [weak self] result in
switch result {
case .success(let sliderAd):
// Display the ad
break
case .failure:
break
}
}
Rendering a native ad slider
After the slider loads, you must render all of its components.
A successfully loaded slider contains one or more related native ads. Ads in the slider must be shown inside a single shared container; otherwise impressions will not be counted.
This approach lets you build the slider layout yourself and place ad components relative to each other. The ad can include both required and optional components. See the full list in Native ad assets.
Tip
For each ad in the slider, we recommend using a layout that includes the full set of possible components. In practice, that tends to improve conversions.
Call bind(with:) on the SliderAd object, passing a container for the slider.
Each ad in the slider is laid out using one of the layout options for standard native ads.
adLoader.loadAd(with: request, options: options) { [weak self] result in
guard case .success(let sliderAd) = result else { return }
sliderAd.delegate = self
// Create a container for the slider; replace NativeAdView with your subclass
let sliderAdView = NativeAdView()
do {
try sliderAd.bind(with: sliderAdView)
} catch {
// Check the error message and fix the issue
return
}
for subAd in sliderAd.ads {
subAd.delegate = self
// Create an ad view for each ad; replace NativeAdView with your subclass
let subAdView = NativeAdView()
do {
try subAd.bind(with: subAdView)
} catch {
return
}
// Add the ad to the container and position it
sliderAdView.addSubview(subAdView)
}
}