iOS SDK - Banner ads
Banner ads are rectangular ads that take up a small part of your app's layout, usually anchored to the top or bottom of the screen. They stay visible while users interact with your app, so they can earn revenue without interrupting the experience.
Before you start, install and initialize the SDK.
Banner refresh
Caution
MDN11 does not currently support banner auto-refresh, so you need to refresh banners manually. Call load periodically, and call show when the banner has loaded.
Refresh only while the banner is on screen. Stop refreshing when the app goes to the background or the user navigates to a screen without the banner, so you don’t request ads nobody sees.
If your current mediation platform (for example MAX, LevelPlay or AdMob) has banner auto-refresh enabled, you have two options: switch to manual refresh as described above, or keep using that platform’s SDK directly for banners until MDN11 supports auto-refresh. Other ad types can still be used via MDN11 in both cases.
1. Get the banner ad object
Get the ad object for your ad unit ID and set its delegate. To use a size other than adaptive, set adSize before calling load(on:). See Banner sizes.
private var banner: BannerAd?
override func viewDidLoad() {
super.viewDidLoad()
banner = A11.shared.getBannerAd(adUnitID: "adUnitId")
banner?.delegate = self
}
2. Implement the delegate
Set the delegate before calling load(on:), so you don't miss any events. When onBannerAdLoaded fires, call show(on:) and add adView to your view hierarchy. show(on:) sizes adView to each new ad, so position adView by its frame after every call. The full example shows positionBanner().
extension MyViewController: BannerAdDelegate {
func onBannerAdLoaded(adInfo: AdInfo) {
banner?.show(on: self)
guard let adView = banner?.adView else { return }
if adView.superview == nil { // Add once, not on every refresh
view.addSubview(adView)
}
// `show(on:)` has adjusted `adView` size to the banner size, so reposition it
positionBanner()
}
func onBannerAdLoadFailed(error: AdLoadError) {
print("Banner load failed: \(error)")
}
func onBannerAdShown(adInfo: AdInfo) {}
func onBannerAdShowFailed(adInfo: AdInfo?, error: AdShowError) {}
func onBannerAdClicked(adInfo: AdInfo) {}
func onBannerAdWillPresentModalView(adInfo: AdInfo) {}
func onBannerAdDidDismissModalView(adInfo: AdInfo) {}
func onBannerAdRevenue(adInfo: AdInfo) {}
}
3. Load the ad
Call load(on:) after setting the delegate, for example at the end of viewDidLoad().
Full example
class MyViewController: UIViewController {
private var banner: BannerAd?
override func viewDidLoad() {
super.viewDidLoad()
banner = A11.shared.getBannerAd(adUnitID: "adUnitId")
banner?.delegate = self
banner?.load(on: self)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
positionBanner() // Keep it centered, for example after rotation
}
// Center at the bottom, using the size set by show(on:)
func positionBanner() {
guard let adView = banner?.adView else { return }
let safeArea = view.safeAreaLayoutGuide.layoutFrame
adView.frame.origin = CGPoint(
x: safeArea.midX - adView.frame.width / 2,
y: safeArea.maxY - adView.frame.height
)
}
}
extension MyViewController: BannerAdDelegate {
func onBannerAdLoaded(adInfo: AdInfo) {
banner?.show(on: self)
if let adView = banner?.adView, adView.superview == nil { // Add once, not on every refresh
view.addSubview(adView)
}
positionBanner()
}
func onBannerAdLoadFailed(error: AdLoadError) {
print("Banner load failed: \(error)")
}
func onBannerAdShown(adInfo: AdInfo) {}
func onBannerAdShowFailed(adInfo: AdInfo?, error: AdShowError) {}
func onBannerAdClicked(adInfo: AdInfo) {}
func onBannerAdWillPresentModalView(adInfo: AdInfo) {}
func onBannerAdDidDismissModalView(adInfo: AdInfo) {}
func onBannerAdRevenue(adInfo: AdInfo) {}
}
Banner sizes
The banner size sets how much space the ad takes up on screen and how its height is calculated. Set adSize before calling load(on:). If you don't set it, the banner uses the adaptive size.
Adaptive sizes only work with ad networks that support them. See Adaptive banner support.
| Size | Value | Description |
|---|---|---|
| Adaptive (default) | BannerAdSize.adaptive |
Full width of the view controller you pass to load(on:). The ad network calculates the height from that width. |
| Standard | BannerAdSize.standard |
Classic fixed-size banner, 50 pt tall on iPhone and 90 pt on iPad. The same height on every device, so it's easy to reserve space for. |
Adaptive banner support
Which ad networks support the adaptive banner sizes. For SDK and adapter versions, see Supported ad networks.
| Ad Network | Adaptive Banner |
|---|---|
| AdMob mediation |
|
| Google Ad Manager mediation |
|
| AppLovin mediation - MAX |