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 characteristics, 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.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)
// 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>"; YMAMutableNativeAdRequestConfiguration *requestConfiguration = [[YMAMutableNativeAdRequestConfiguration alloc] initWithAdUnitID:@"R-M-XXXXXX"]; requestConfiguration.age = age; requestConfiguration.contextQuery = contextQuery; requestConfiguration.contextTags = contextTags; requestConfiguration.gender = gender; requestConfiguration.location = location; requestConfiguration.parameters = parameters; [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.
- 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
- 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"
// 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).
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).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)
// 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>"; YMAMutableNativeAdRequestConfiguration *requestConfiguration = [[YMAMutableNativeAdRequestConfiguration alloc] initWithAdUnitID:@"R-M-XXXXXX"]; requestConfiguration.age = age; requestConfiguration.contextQuery = contextQuery; requestConfiguration.contextTags = contextTags; requestConfiguration.gender = gender; requestConfiguration.location = location; requestConfiguration.parameters = parameters; [adLoader loadAdWithRequestConfiguration:requestConfiguration adsCount:adsCount];
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 the images themselves, call the loadImages method on the received 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];