Loading ads

Warning.

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

  1. Loading ads
  2. Loading multiple ads
  3. Ways to load images

Loading ads

  1. Create an instance of the YMANativeAdLoader class to get native ads.

  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.

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 
}

Examples with a demo AdUnitId

To see how the ad will be displayed in the app, use 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).

  1. Create an instance of the YMANativeBulkAdLoader class to get native ads.

  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 characteristics, and other data that might improve the quality of ad selection.

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

  4. To track the ad loading process, implement the YMANativeBulkAdLoaderDelegate protocol methods: -nativeBulkAdLoader:didLoadAds: and -nativeBulkAdLoader:didFailLoadingWithError:.

  5. Send to the loader the request configuration and count of ads requested (the adsCount parameter).

    adLoader.loadAds(with: 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".
}
Note.

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

The resulting ad will have the images ready. They are stored in the device memory until the ad is destroyed.

Notifications about image loading

Restriction. You can only get notifications when loading images manually.

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")
}