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 characteristics, 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.

    Use the YMAMutableNativeAdRequestConfiguration class to transmit the code received in the Adfox interface (for more information, see Help for Adfox).
    // Code from the Adfox interface for working with direct campaigns.
    var parameters = [String: String]()
    parameters["adf_ownerid"] = "<example>"
    parameters["adf_p1"] = "<example>"
    parameters["adf_p2"] = "<example>"
    parameters["adf_pfc"] = "<example>"
    parameters["adf_pfb"] = "<example>"
    parameters["adf_pt"] = "<example>"
    parameters["adf_pd"] = "<example>"
    parameters["adf_pw"] = "<example>"
    parameters["adf_pv"] = "<example>"
    parameters["adf_prr"] = "<example>"
    parameters["adf_pdw"] = "<example>"
    parameters["adf_pdh"] = "<example>"
    let requestConfiguration = YMAMutableNativeAdRequestConfiguration(adUnitID: "R-M-XXXXXX")
    requestConfiguration.age = age
    requestConfiguration.contextQuery = contextQuery
    requestConfiguration.contextTags = contextTags
    requestConfiguration.gender = gender
    requestConfiguration.location = location
    requestConfiguration.parameters = parameters
    
    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.

  8. Optionally, you can use the (nonatomic, copy, readonly) NSString *info property of the YMANativeAd protocol to get the string that was set in the Additional text box in the Adfox interface.

Examples with a demo AdUnitId

To see how the ad will be displayed in the app, use a demo AdUnitId:
  • For an App Install type of ad: demo-native-app-yandex.
  • For a Content type of ad: demo-native-content-yandex.
  • For an Image type of ad: R-M-187883-1. You also need to pass a list of parameters:
    // Code from the Adfox interface for working with direct campaigns.
    parameters["adf_ownerid"] = "168627"
    parameters["adf_p1"] = "bvyhu"
    parameters["adf_p2"] = "fksh"
    parameters["adf_pt"] = "b"

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).

    Use the YMAMutableNativeAdRequestConfiguration class to transmit the code received in the Adfox interface (for more information, see Help for Adfox).
    // Code from the Adfox interface for working with direct campaigns.
    var parameters = [String: String]()
    parameters["adf_ownerid"] = "<example>"
    parameters["adf_p1"] = "<example>"
    parameters["adf_p2"] = "<example>"
    parameters["adf_pfc"] = "<example>"
    parameters["adf_pfb"] = "<example>"
    parameters["adf_pt"] = "<example>"
    parameters["adf_pd"] = "<example>"
    parameters["adf_pw"] = "<example>"
    parameters["adf_pv"] = "<example>"
    parameters["adf_prr"] = "<example>"
    parameters["adf_pdw"] = "<example>"
    parameters["adf_pdh"] = "<example>"
    let requestConfiguration = YMAMutableNativeAdRequestConfiguration(adUnitID: "R-M-XXXXXX")
    requestConfiguration.age = age
    requestConfiguration.contextQuery = contextQuery
    requestConfiguration.contextTags = contextTags
    requestConfiguration.gender = gender
    requestConfiguration.location = location
    requestConfiguration.parameters = parameters
    
    adLoader.loadAds(with: requestConfiguration, adsCount: adsCount)
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")
}