Ad slider
Native advertising is a type of ad whose layout can be defined at the app level. This feature allows you to change the visual style of ads and their placement, in the context of the app design specifics.
Ad rendering is performed with native platform tools, which enhances ad performance and quality.
Native ads enhance the overall ad experience, so you can show more ads while keeping users engaged. In the long run, this allows you to maximize your advertising revenue.
Prerequisite
- Follow the SDK integration steps described under Quick start.
- First, you need to initialize the advertising SDK.
- Make sure you're using the latest version of the Yandex Mobile Ads SDK, and if you're using mediation, 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 the
loadAd(with:options:completion:)method to load an ad. -
When the ad loads, use the
SliderAdobject to display it. -
If an error occurs, resolve it.
let adLoader = SliderAdLoader()
let request = AdRequest(adUnitID: "<AdUnitID>")
let options = NativeAdOptions()
do {
let sliderAd = try await adLoader.loadAd(with: request, options: options)
// Show 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):
// Render the ad
break
case .failure:
break
}
}
Rendering a native ad slider
Once the slider is loaded, you must render all its assets.
A successfully loaded slider contains one or more native ads having the same context. Ads in the slider must be displayed within the same shared container: otherwise, ad impressions won't count.
With this method, you can arrange the slider for your native ads by positioning ad components relative to each other. The ad may include both required and optional assets for display. For the full list, see Native ad assets.
Tip
For each ad in the slider, we recommend using a layout that includes a complete set of possible assets. In practical terms, such layouts result in higher conversion rates.
Call the bind(with:) method on the SliderAd object, passing the ad slider container.
Each ad in the slider is laid out using a standard native advertising layout method.
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 resolve 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)
}
}