Loading ads
This is an archived version of the documentation. Actual documentation for all platforms can be found here.
Loading ads
Create an instance of the YMANativeAdLoader class to get native ads.
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.
- General ad request
-
// 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 }
Examples with a demo AdUnitId
- For App Install ads: demo-native-app-yandex.
- For Content ads: demo-native-content-yandex.
- For video ads: demo-native-video-yandex.
Loading multiple ads
Yandex Mobile Ads SDK provides the option to load multiple ads in a single request (up to nine ads).
Create an instance of the YMANativeBulkAdLoader class to get native ads.
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 characteristics, and other data that might improve the quality of ad selection.Set a delegate for retrieving an ad that implements the YMANativeBulkAdLoaderDelegate protocol.
To track the ad loading process, implement the YMANativeBulkAdLoaderDelegate protocol methods: -nativeBulkAdLoader:didLoadAds: and -nativeBulkAdLoader:didFailLoadingWithError:.
Send to the loader the request configuration and count of ads requested (the
adsCount
parameter).adLoader.loadAds(with: requestConfiguration, adsCount: adsCount)
[self.adLoader loadAdsWithRequestConfiguration:requestConfiguration adsCount:adsCount];
// Creating a request configuration
let requestConfiguration = YMAMutableNativeAdRequestConfiguration(adUnitID: AdUnitID)
// Creating a loader
adLoader = YMANativeBulkAdLoader()
adLoader.delegate = self
// Passing the request configuration and the number of requested ads to the loader
adLoader.loadAds(with: requestConfiguration, adsCount: adsCount)
// Implementing delegate methods
func nativeBulkAdLoader(_ nativeBulkAdLoader: YMANativeBulkAdLoader, didLoad ads: [YMANativeAd]) {
...
// Working with each id<YMANativeAd> object separately. For more information, see "Customizing the ad design".
}
// Creating a request configuration
YMAMNativeAdRequestConfiguration *requestConfiguration =
[[YMANativeAdRequestConfiguration alloc] initWithAdUnitID:AdUnitID];
// Creating a loader
self.adLoader = [[YMANativeBulkAdLoader alloc] init];
self.adLoader.delegate = self;
// Passing the request configuration and count of requested ads to the loader
[self.adLoader loadAdsWithRequestConfiguration:requestConfiguration adsCount:adsCount];
// Implementing delegate methods
- (void)nativeBulkAdLoader:(YMANativeBulkAdLoader *)nativeBulkAdLoader didLoadAds:(NSArray<idYMANativeAd> *)ads
{
...
// Working with each id<YMANativeAd> object separately. For more information, see "Customizing the ad design".
}
Yandex Mobile Ads SDK doesn't guarantee that the requested number of ads will be loaded. The resulting array will contain from 0 to adsCount NativeAd objects. You can render all the received ad objects separately from each other using the above methods for laying out native ads.
Ways to load images
If the app simultaneously stores links to just one or a small number of ads, we recommend using automatic loading.
When creating the configuration, set shouldLoadImagesAutomatically to YES
:
let requestConfiguration = YMAMutableNativeAdRequestConfiguration(adUnitID: AdUnitID)
requestConfiguration.shouldLoadImagesAutomatically = true
YMAMutableNativeAdRequestConfiguration *requestConfiguration =
[[YMAMutableNativeAdRequestConfiguration alloc] initWithAdUnitID:AdUnitID];
requestConfiguration.shouldLoadImagesAutomatically = YES;
The resulting ad will have the images ready. They are stored in the device memory until the ad is destroyed.
If the app simultaneously stores links to a large number of ads, we recommend using manual image loading.
When creating the configuration, set shouldLoadImagesAutomatically to NO
:
let requestConfiguration = YMAMutableNativeAdRequestConfiguration(adUnitID: AdUnitID)
requestConfiguration.shouldLoadImagesAutomatically = false
YMAMutableNativeAdRequestConfiguration *requestConfiguration =
[[YMAMutableNativeAdRequestConfiguration alloc] initWithAdUnitID:AdUnitID];
requestConfiguration.shouldLoadImagesAutomatically = NO;
The resulting ad will only have the image sizes. To load images in the ad, call the loadImages method on the resulting ad.
func showAd() {
// Show ad: custom native view or template
view.addSubview(adView)
adView.ad?.loadImages()
}
- (void)showAd
{
// Show ad: custom native view or template
[self.view addSubview:self.adView];
[self.adView.ad loadImages];
}
Notifications about image loading
To enable notifications that are sent when an image is loaded, call the -addImageLoadingObserver: method.
ad?.add(self)
...
func nativeAdDidFinishLoadingImages(_ ad: YMANativeAd) {
print("Finished loading images")
}
[ad addImageLoadingObserver:self];
...
- (void)nativeAdDidFinishLoadingImages:(id<YMANativeAd>)ad
{
NSLog(@"Finished loading images");
}
ad?.remove(self)
[ad removeImageLoadingObserver:self];