Loading ads

  1. Add the import:
    #import <YandexMobileAds/YandexMobileNativeAds.h>
  2. Decide how to load images for showing ads. Images can be loaded automatically or manually. The loading method is set in the shouldLoadImagesAutomatically property.
  3. Create a configuration using the YMANativeAdLoaderConfiguration class. When initializing an instance of this class, pass the required properties: blockID and shouldLoadImagesAutomatically.

    Block ID is a unique identifier in the adf-XXXXXX/YYYYYY format, which is assigned in the Partner interface.
    Attention.

    In the mobile mediation interface, the Block ID is called the location ID.

  4. Optionally, you can set the preferred image size classes in configurations. Each class corresponds to a specific set of image formats. Sizes are set in the imageSizes property.
    Note.

    By default, all size classes are requested. This means that the response from the SDK may contain an ad with image sizes from 113x150 to 1080x608.

    NSArray *imageSizes = @[ kYMANativeImageSizeMedium, kYMANativeImageSizeLarge ];
    YMANativeAdLoaderConfiguration *configuration =
        [[YMANativeAdLoaderConfiguration alloc] initWithBlockID:@"R-M-XXXXXX"
                                  shouldLoadImagesAutomatically:NO];
    Image classes and formats
    kYMANativeImageSizeSmall class
    Fixed size Allowed height to width ratio Description
    Height — 150 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
    A small image that is positioned next to the ad.
    kYMANativeImageSizeMedium class
    Fixed size Allowed height to width ratio Description
    Width — 600 pixels
    • 16:9
    A medium-sized image that is positioned next to the ad.
    Width — 450 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
    Height — 450 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
    Width — 300 pixels
    • 16:9
    kYMANativeImageSizeLarge class
    Fixed size Allowed height to width ratio Description
    Width — 1080 pixels
    • 16:9
    A large image that is positioned:
    • in the center of the ad and covers the entire width of the ad or most of the width.
    • at the bottom of the ad and covers the entire width of the ad or most of the width.
    Width — 600 pixels
    • 16:9
    Height — 450 pixels
    • 1:1
    • 3:4
    • 4:3
    • 16:9
  5. Use the YMANativeAdLoader class to create a loader for native ads. When initializing an object of this class, pass the configuration as a parameter:
    YMANativeAdLoader *adLoader =
        [[YMANativeAdLoader alloc] initWithConfiguration:configuration];
  6. Set a delegate for retrieving an ad that implements the YMANativeAdLoaderDelegate protocol:
    adLoader.delegate = self;
  7. To load the ad, send the loader the loadAdWithRequest: message. Optionally, you can send a request containing data for targeting.
    [adLoader loadAdWithRequest:nil];
  8. You can optionally enable logging by using the enableLogging method. If an impression wasn't registered, a message appears in the console.

  9. If the ad loaded, one of the following methods is called:

    For Content:
    - (void)nativeAdLoader:(null_unspecified YMANativeAdLoader *)loader didLoadContentAd:(id<YMANativeContentAd>)ad
    For App Install:
    - (void)nativeAdLoader:(null_unspecified YMANativeAdLoader *)loader didLoadAppInstallAd:(id<YMANativeAppInstallAd>)ad
    For Image:
    - (void)nativeAdLoader:(null_unspecified YMANativeAdLoader *)loader didLoadImageAd:(id<YMANativeImageAd>)ad
  10. If the ad didn't load, the following method is called:

    - (void)nativeAdLoader:(null_unspecified YMANativeAdLoader *)loader didFailLoadingWithError:(NSError *)error

    For more information about possible errors, see the section YMANativeErrorCode.

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:

YMANativeAdLoaderConfiguration *configuration =
    [[YMANativeAdLoaderConfiguration alloc] initWithBlockID:@"R-M-XXXXXX"
                              shouldLoadImagesAutomatically:YES];

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 addImageLoadingObserver:self];
...
- (void)nativeAdDidFinishLoadingImages:(id<YMANativeGenericAd>)ad
{
    NSLog(@"Finished loading images");
}