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 R-M-XXXXXX-Y format, which is assigned in the Partner interface.

  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.

    Use the YMAAdRequest class to transmit the code received in the ADFOX interface (for more information, see the Help for ADFOX Mobile).
    // Code from the ADFOX interface for working with direct campaigns.
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    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>";
    YMAAdRequest *adRequest =
        [[YMAAdRequest alloc] initWithLocation:nil
                                  contextQuery:nil
                                   contextTags:nil
                                    parameters:parameters];
    [adLoader loadAdWithRequest:adRequest];
  8. 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
  9. 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.

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

Examples with a demo Block ID

To see how the ad will be displayed in the app, use a demo Block ID:
  • For an App Install type of ad: R-M-DEMO-native-i.
  • For a Content type of ad: R-M-DEMO-native-c.
  • 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";

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