Enabling banner ads

Warning.

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

  1. Banner types
  2. Enabling a banner
  3. Example of working with banners

Banner types

Features:

  1. The specified banner width is used. The height is selected automatically.
  2. The width of banners is set using the +stickySizeWithContainerWidth: method.
  3. The banner height shouldn't exceed 15% of the device height and should be at least 50 dp.

Examples of displaying banners:

Enabling a banner

To enable the banner:

  1. Add the import:

    import YandexMobileAds
  2. Create @property, where the link to the banner ad will be stored:

    var adView: YMAAdView!
  3. Create a banner:

    To set the width of a banner, call the +stickySizeWithContainerWidth: method.

    let adSize = YMAAdSize.stickySize(withContainerWidth: width)
    let adView = YMAAdView(adUnitID: "", adSize: adSize)
    adView.delegate = self
    Restriction. Banner size requirements when displaying video ads

    Minimum size of a banner that supports video playback is 300x160 or 160x300.

    AdUnitId is a unique identifier in R-M-XXXXXX-Y format, which is assigned in the Partner Interface.

    In addition, self must conform to the YMAAdViewDelegate protocol. If the delegate implements the -viewControllerForPresentingModalView method, links open in the in-app browser. Otherwise, links open in the browser installed on the device.

    To find out why ads aren't working correctly, use the -adViewDidFailLoading:error: method.

    For error descriptions, see YMAAdErrorCode.

  4. Display a banner. There are two ways to place a banner:

    • Using autolayout constraints.

      Add the banner to UIView. Then add autolayout constraints so the banner is displayed in the desired location.
      view.addSubview(adView)
      adView.translatesAutoresizingMaskIntoConstraints = false
      var adViewConstraints = [
          adView.leadingAnchor.constraint(equalTo: adView.superview!.leadingAnchor),
          adView.trailingAnchor.constraint(equalTo: adView.superview!.trailingAnchor)
      ]
      let bottomDistance: CGFloat = 8
      if #available(iOS 11.0, *) {
          adViewConstraints.append(
              adView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: bottomDistance)
          )
      } else {
          adViewConstraints.append(
              adView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: bottomDistance)
          )
      }
      NSLayoutConstraint.activate(adViewConstraints)
    • Using the following methods:
      displayAtTop(in:)
      displayAtBottom(in:)

      In both cases, banners are centered horizontally.

  5. Load the banner. Optionally, you can use the YMAAdRequest class to transmit the data for targeting.

    func loadAd(with request: YMAAdRequest?)

    When the banner loads, the delegate is notified.

  6. You can optionally enable logging by using the +enableLogging method. If an impression wasn't registered, a message appears in the console.

  7. Optionally, you can set up notifications about the end of video playback in banner ads.

    Usage example
    // Getting an instance of YMAVideoController using VideoController.
    let videoController = adView.videoController
    
    // Setting up a delegate that implements the YMAVideoDelegate protocol.
    videoController.delegate = self
    
    // Implementing the YMAVideoDelegate protocol method.
    // MARK: - YMAVideoDelegate
    func videoControllerDidFinishPlayingVideo(_ videoController: YMAVideoController) {
        print("Video complete");
    }

If an ad is integrated this way, the banner appears after the app starts.

To see how the banner ad will be displayed in the app, use a demo AdUnitId:
  • demo-banner-yandex

Example of working with banners

The following code demonstrates creating and configuring the AdView object, registering a listener, and loading a banner.

override func viewDidLoad() {
    super.viewDidLoad()

    // Setting the ad width
    let adSize = YMAAdSize.stickySize(withContainerWidth: containerWidth())

    // Creating an adView instance     adView = YMAAdView(adUnitID: "R-M-XXXXX-YY", adSize: adSize)
    adView.delegate = self
    addAdView()

    // Loading the ad     adView.loadAd()
}

private func addAdView() {
    view.addSubview(adView)
    adView.translatesAutoresizingMaskIntoConstraints = false
    var adViewConstraints = [
        adView.leadingAnchor.constraint(equalTo: adView.superview!.leadingAnchor),
        adView.trailingAnchor.constraint(equalTo: adView.superview!.trailingAnchor)
    ]
    let bottomDistance: CGFloat = 8
    if #available(iOS 11.0, *) {
        adViewConstraints.append(
            adView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: bottomDistance)
        )
    } else {
        adViewConstraints.append(
            adView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: bottomDistance)
        )
    }
    NSLayoutConstraint.activate(adViewConstraints)
}

private func containerWidth() -> CGFloat {
    var containerWidth = view.frame.width
    if #available(iOS 11, *) {
        containerWidth = view.frame.inset(by: view.safeAreaInsets).width
    }
    return containerWidth
}