Adaptive inline banner
Adaptive inline banners are a flexible format of banner advertising, providing maximum efficiency by optimizing the size of the ad on each device.
This ad type lets developers set a maximum allowable width and height for the ad, though the optimal ad size is still determined automatically. To select the best ad size, built-in adaptive banners use the maximum height rather than the fixed height. That provides room for improving performance.
Typically, that format is used in feed-based apps or contexts where it's okay to focus primarily on the ad.
Appearance
This guide shows how to integrate adaptive inline banners into iOS apps. In addition to code examples and instructions, it contains format-specific recommendations and links to additional resources.
Prerequisite
- Follow the SDK integration steps described in Quick start.
- Initialize your ad SDK in advance.
- Make sure you're running the latest Yandex Mobile Ads SDK version. If you're using mediation, make sure you're also running the latest version of the unified build.
Implementation
Key steps for integrating adaptive inline banners:
- Create a
BannerAdViewinstance. - Implement the delegate methods.
- Load the ad.
- Pass additional settings if you're using Adfox.
- Receive the ad in the delegate method and render it.
Features of adaptive inline banner integration
-
All calls to Yandex Mobile Ads SDK methods must be made from the main thread.
-
We strongly advise against attempting to load a new ad when receiving an error in the
func bannerAdViewDidFailLoading(_ bannerAdView: BannerAdView, error: Error)delegate method. If you need to load an ad fromfunc bannerAdViewDidFailLoading(_ bannerAdView: BannerAdView, error: Error), restrict ad load retries to avoid recurring failed ad requests due to network connection constraints. -
To make sure your adaptive inline banners work correctly, use Auto Layout. Using a fixed-sized frame for the view may result in incorrect ad rendering.
-
Adaptive inline banners work best when using all the available width. In most cases, that's the full width of the device screen. Make sure to include all padding and safe display areas applicable to your app.
-
Adaptive inline banners are designed for placement in scrollable content. It can have the same height as the device screen or be limited to the maximum height depending on the API.
-
To get the ad size, use the
BannerAdSize.inline(width: CGFloat, maxHeight: CGFloat)method, passing the available width of the ad container and the maximum allowed ad height as arguments. -
The
BannerAdSizeobject calculated usingBannerAdSize.inline(width: CGFloat, maxHeight: CGFloat)contains constant ad width and height values for the same device. Once you test your app layout on a specific device, you can be sure the ad size will not change.
Creating a BannerAdView instance
To display banner ads, create a BannerAdView instance, passing only the ad size. You also need to set a delegate for BannerAdView by implementing the BannerAdViewDelegate protocol in your class.
Ads are sized for the device using the SDK method BannerAdSize.inline(width: CGFloat, maxHeight: CGFloat). Pass the maximum permissible width of the ad container. We recommend using the entire width of the device screen or the width of the parent container. Make sure you include all padding and safe display areas applicable to your app.
You will also need the ad unit ID (adUnitId) from the Yandex Advertising Network interface.
Example of creating a BannerAdView in a view controller:
final class InlineBannerViewController: UIViewController {
private lazy var bannerAdView: BannerAdView = {
let adSize = BannerAdSize.inline(width: 320, maxHeight: 320)
let bannerAdView = BannerAdView(adSize: adSize)
bannerAdView.delegate = self
bannerAdView.translatesAutoresizingMaskIntoConstraints = false
return bannerAdView
}()
}
extension InlineBannerViewController: BannerAdViewDelegate {
func bannerAdViewDidLoad(_ bannerAdView: BannerAdView) {
// Ad loaded successfully
}
func bannerAdViewDidFailLoading(_ bannerAdView: BannerAdView, error: Error) {
// Failed to load
}
}
Loading ads
After you create BannerAdView, you need to load the ad.
To get notified about successful or failed loads and to track the adaptive inline banner lifecycle, set the delegate on the BannerAdView instance and implement BannerAdViewDelegate.
You can extend the ad request using AdRequest by passing user interests, page context, location, or other data. Additional contextual data can significantly improve ad quality. Read more in Ad targeting.
The following example shows how to load an adaptive inline banner. After a successful load, func bannerAdViewDidLoad(_ bannerAdView: BannerAdView) of BannerAdViewDelegate is called:
final class InlineBannerViewController: UIViewController {
private lazy var bannerAdView: BannerAdView = {
let adSize = BannerAdSize.inline(width: 320, maxHeight: 320)
let bannerAdView = BannerAdView(adSize: adSize)
bannerAdView.delegate = self
bannerAdView.translatesAutoresizingMaskIntoConstraints = false
return bannerAdView
}()
func loadAd() {
let request = AdRequest(adUnitID: "R-M-XXXXX-YY")
bannerAdView.loadAd(with: request)
}
}
Displaying ads
After the ad loads successfully, add it to the view hierarchy using Auto Layout constraints.
Add bannerAdView from the delegate callback to your container, then add constraints so the banner appears where you need it.
final class InlineBannerViewController: UIViewController {
private lazy var bannerAdView: BannerAdView = {
let adSize = BannerAdSize.inline(width: 320, maxHeight: 320)
let bannerAdView = BannerAdView(adSize: adSize)
bannerAdView.delegate = self
bannerAdView.translatesAutoresizingMaskIntoConstraints = false
return bannerAdView
}()
func showAd() {
view.addSubview(bannerAdView)
NSLayoutConstraint.activate([
bannerAdView.topAnchor.constraint(equalTo: loadButton.bottomAnchor, constant: 100),
bannerAdView.centerXAnchor.constraint(equalTo: view.centerXAnchor)
])
}
}
Testing adaptive inline banner integration
Using demo ad units for ad testing
We recommend using test ads to test your ad integration and your app itself.
To guarantee that test ads are returned for every ad request, we created a special demo ad placement ID. Use it to check your ad integration.
Demo adUnitId: demo-banner-yandex.
Warning
Before publishing your app to the store, make sure you replace the demo ad unit ID with a real one obtained from the Yandex Advertising Network interface.
You can find the list of available demo ad placement IDs in the Demo ad units for testing section.
Testing ad integration
You can test your ad integration using the native Console tool.
To view detailed logs, call the YMAMobileAds class's enableLogging method.
YMAMobileAds.enableLogging()
To view SDK logs, go to the Console tool and set Subsystem = com.mobile.ads.ads.sdk. You can also filter logs by category and error level.
If you're having problems integrating ads, you'll get a detailed report on the issues and recommendations for how to fix them.
Additional resources
-
Link to GitHub.