MediaView integration guide

Warning.

This is an archived version of the documentation. Actual documentation for all platforms can be found here.

Note.

Integration is described using the example of an ImageAd.

  1. Enable the Mobile Ads SDK.

    Add dependencies to the Podfile project:
    pod 'YandexMobileAds', '5.9.1'
    pod 'YandexMobileAdsInstream', '0.18.0'
  2. Load the native ad.

    1. Create an object of the YMANativeAdLoader class with the configuration YMANativeAdRequestConfiguration.
    2. Implement the YMANativeAdLoaderDelegate protocol and the -nativeAdLoader:didLoadAd: method.
    3. Set a delegate for the created YMANativeAdLoader object.
  3. Display the native ad.

    Restriction. Requirements for the size of mediaView when displaying video ads

    Minimum 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 appropriate mediaView height value, use the aspectRatio property value. When displaying ads using templates, the correct height for mediaView will be calculated automatically based on the width to height ratio.

    1. For an easy way to display an ad, use the templates.
      1. Create an object of the YMANativeBannerView class.
      2. 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)
      }

      For more information about native advertising, see Native ads.

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

      For more information about native advertising, see Layout without a template.