Banner ads
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.
Banner types
Features:
- The specified banner width is used. The height is selected automatically.
- The width of banners is set using the +stickySizeWithContainerWidth: method.
- The banner height shouldn't exceed 15% of the device height and should be at least 50 dp.
Examples of displaying banners:
Features:
- A banner fills up the entire unit using the set maximum sizes.
- The width and height of banners is set using the +flexibleSizeWithCGSize: method.
- The SDK imposes
NSLayoutConstraint
on the banner height.
Examples of displaying banners:
Enabling a banner
To enable the banner:
Add the import:
import YandexMobileAds
#import <YandexMobileAds/YandexMobileAds.h>
Create @property, where the link to the banner ad will be stored:
var adView: YMAAdView!
@property (nonatomic, strong) YMAAdView *adView;
- 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
YMAAdSize adSize = [YMAAdSize stickySizeWithContainerWidth:width]; YMAAdView *adView = [[YMAAdView alloc] initWithAdUnitID:<AdUnitID> adSize:adSize]; adView.delegate = self;
To set the width and height of a banner, call the +flexibleSizeWithCGSize: method.
let adSize = YMAAdSize.flexibleSize(with: size) let adView = YMAAdView(adUnitID: "", adSize: adSize) adView.delegate = self
YMAAdSize adSize = [YMAAdSize flexibleSizeWithCGSize:size]; YMAAdView *adView = [[YMAAdView alloc] initWithAdUnitID:<AdUnitID> adSize:adSize]; adView.delegate = self;
Restriction. Banner size requirements when displaying video adsMinimum 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.
Display a banner. There are two ways to place a banner:
Using
autolayout constraints
.Add the banner toUIView
. Then addautolayout 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)
UIView *adView = self.adView; [self.view addSubview:adView]; adView.translatesAutoresizingMaskIntoConstraints = NO; NSMutableArray *adViewConstraints = [NSMutableArray arrayWithArray:@[ [adView.leadingAnchor constraintEqualToAnchor:adView.superview.leadingAnchor], [adView.trailingAnchor constraintEqualToAnchor:adView.superview.trailingAnchor] ]]; int bottomDistance = 8; if (@available(iOS 11.0, *)) { UILayoutGuide *guide = self.view.safeAreaLayoutGuide; [adViewConstraints addObject:[adView.bottomAnchor constraintEqualToAnchor:guide.bottomAnchor constant:bottomDistance]]; } else { [adViewConstraints addObject:[adView.bottomAnchor constraintEqualToAnchor:adView.superview.bottomAnchor constant:bottomDistance]]; } [NSLayoutConstraint activateConstraints:adViewConstraints];
- Using the following methods:
In both cases, banners are centered horizontally.
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)
// 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>"; YMAMutableAdRequest *request = [[YMAMutableAdRequest alloc] init]; request.age = age; request.contextQuery = contextQuery; request.contextTags = contextTags; request.gender = gender; request.location = location; request.parameters = parameters; [self.adView loadAdWithRequest:adRequest];
You can optionally enable logging by using the +enableLogging method. If an impression wasn't registered, a message appears in the console.
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"); }
// Getting an instance of YMAVideoController using videoController. YMAVideoController *videoController = self.adView.videoController; // Setting up a delegate that implements the YMAVideoDelegate method. videoController.delegate = self; // Implementing the YMAVideoDelegate protocol method. #pragma mark - YMAVideoDelegate - (void)videoControllerDidFinishPlayingVideo:(YMAVideoController *)videoController { NSLog(@"%@", @"Video complete"); }