MediaView integration guide
This is an archived version of the documentation. Actual documentation for all platforms can be found here.
Integration is described using the example of an ImageAd
.
Enable the Mobile Ads SDK.
Add dependencies to thePodfile
project:pod 'YandexMobileAds', '5.9.1' pod 'YandexMobileAdsInstream', '0.18.0'
Load the native ad.
- Create an object of the YMANativeAdLoader class with the configuration YMANativeAdRequestConfiguration.
- Implement the YMANativeAdLoaderDelegate protocol and the -nativeAdLoader:didLoadAd: method.
- Set a
delegate
for the createdYMANativeAdLoader
object. - Call the -loadAdWithRequestConfiguration: method.
Display the native ad.
Restriction. Requirements for the size of mediaView when displaying video adsMinimum size of an instance of the YMANativeMediaView class, which supports video playback: 300x160 or 160x300.
To support video playback in native ads, we recommend setting the width for
mediaView
to at least 300. To calculate the appropriatemediaView
height value, use the aspectRatio property value. When displaying ads using templates, the correct height formediaView
will be calculated automatically based on the width to height ratio.- For an easy way to display an ad, use the templates.
- Create an object of the YMANativeBannerView class.
- Set the loaded ad object for it.
override func viewDidLoad() { super.viewDidLoad() adLoader = YMANativeAdLoader() adLoader.delegate = self let requestConfiguration = YMANativeAdRequestConfiguration(adUnitID: “<AdUnitID>”) adLoader.loadAd(with: requestConfiguration) }
- (void)viewDidLoad { [super viewDidLoad]; self.adLoader = [[YMANativeAdLoader alloc] init]; self.adLoader.delegate = self; YMANativeAdRequestConfiguration *requestConfiguration = [[YMANativeAdRequestConfiguration alloc] initWithAdUnitID:@"your_AdUnitID"]; [self.adLoader loadAdWithRequestConfiguration:requestConfiguration]; }done
For more information about native advertising, see Native ads.
You can also use the manual method to display native ads. This allows you to adapt native ads to your main content as closely as possible. Call the -bindWithAdView:error: method.
func nativeAdLoader(_ loader: YMANativeAdLoader, didLoad ad: YMANativeAd) { ad.delegate = self do { try ad.bind(with: adView) } catch { print("Error: \(error)") } } func configureView(for ad: YMANativeAd) { let assets = ad.adAssets() if let media = assets.media { //you can use the aspect ratio if you need it to determine the size of media view. print(String(format: "Media aspect ratio: %.2f", media.aspectRatio)) } }
- (void)nativeAdLoader:(YMANativeAdLoader *)loader didLoadAd:(id<YMANativeAd>)ad { ad.delegate = self; NSError *error = nil; BOOL result = [ad bindWithAdView:self.adView error:error]; if (error != nil) { NSLog(@"Error: %@", error); } } - (void)configureViewForAd:(id<YMANativeAd>)ad { YMANativeAdAssets *assets = [ad adAssets]; YMANativeAdMedia *media = assets.media; if (media != nil) { //you can use the aspect ratio if you need it to determine the size of media view. NSLog(@"Media aspect ratio: %.2f", media.aspectRatio); } }
For more information about native advertising, see Layout without a template.