Banner ads

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

To enable the banner:

  1. Add the import:

    #import <YandexMobileAds/YandexMobileAds.h>
  2. Create @property, where the link to the banner ad will be stored:

    @property (nonatomic, strong) YMAAdView *adView;
  3. Specify the banner size and initialize the banner in the app:

    self.adView = [[YMAAdView alloc] initWithBlockID:<your unique BlockID>
    adSize:<Size of the YMAAdSize class> delegate:self];
    Restriction. Requirements for banner size when displaying video ads

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

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

    To determine why advertising is not working correctly, use the - (void)adViewDidFailLoading:(YMAAdView *)adView error:(NSError *)error method.

    For error descriptions, see YMAAdErrorCode.

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

    • Using autolayout constraints. To place the banner, add it to UIView:
       [self.view addSubview:self.adView];

      Then add autolayout constraints so the banner is displayed in the desired location. In the example below, two autolayout constraints are added — vertical centering and horizontal centering.

      [self.adView setTranslatesAutoresizingMaskIntoConstraints:NO];
       NSLayoutConstraint *centerHorizontally =
          [NSLayoutConstraint constraintWithItem:self.adView
                                       attribute:NSLayoutAttributeCenterX
                                       relatedBy:NSLayoutRelationEqual
                                          toItem:self.view
                                       attribute:NSLayoutAttributeCenterX
                                      multiplier:1.f
                                        constant:0.f];
      NSLayoutConstraint *centerVertically =
          [NSLayoutConstraint constraintWithItem:self.adView
                                       attribute:NSLayoutAttributeCenterY
                                       relatedBy:NSLayoutRelationEqual
                                          toItem:self.view
                                       attribute:NSLayoutAttributeCenterY
                                      multiplier:1.f
                                        constant:0.f];
      
      [self.view addConstraint:centerVertically];
      [self.view addConstraint:centerHorizontally];
    • Using the following methods:

      - (void)displayAtTopInView:(UIView *)view;
      - (void)displayAtBottomInView:(UIView *)view;

      In both cases, banners are centered horizontally.

  5. Load the banner using the – loadAdWithRequest: method.

    // 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>";
    YMAAdRequest *adRequest = [[YMAAdRequest alloc] initWithLocation:nil
                                                        contextQuery:nil
                                                         contextTags:nil
                                                          parameters:parameters];
    [self.adView loadAdWithRequest:adRequest];
  6. Optionally, you can set up notifications about the end of video playback in banner ads.
    Usage example
    //  Getting an instance of YMAVideoController using videoController.
    YMAVideoController *videoController = self.adView.videoController;
    
    // Setting up a delegate that implements the YMAVideoDelegate protocol.
    videoController. delegate = self;
    
    // Implementing the YMAVideoDelegate protocol method.
    #pragma mark - YMAVideoDelegate
    - (void)videoControllerDidFinishPlayingVideo:(YMAVideoController *)videoController
    {
        NSLog(@"%@", @"Video complete");
    }