Banner ads

Warning.

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

A banner is a configurable ad that covers part of the screen and reacts to clicks.

  1. Banner types
  2. Enabling a banner

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 using the -loadAdWithRequest: method.

    Use the YMAAdRequest 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 request = YMAMutableAdRequest()
    request.age = age
    request.contextQuery = contextQuery
    request.contextTags = contextTags
    request.gender = gender
    request.location = location
    request.parameters = parameters
    adView.loadAd(with: adRequest)
  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");
    }