Overview
Banner ads are rectangular ads displayed in a portion of the screen. They can be used in both UIKit and SwiftUI.
Key Features
- Can be fixed at the top, bottom, or middle of the screen
- Supports both image and video ads
- Supports both UIKit and SwiftUI
- Ad event handling through delegates
Use test unit ID in development environment: PUBLIC_TEST_UNIT_ID_320_100
UIKit Implementation
In UIKit environment, implement banner ads using the AdropBanner class.
Basic Implementation
import UIKit
import AdropAds
class ViewController: UIViewController {
private var banner: AdropBanner?
override func viewDidLoad() {
super.viewDidLoad()
// 1. Create banner instance
banner = AdropBanner(unitId: "YOUR_UNIT_ID")
// 2. Set delegate
banner?.delegate = self
// 3. Add to view hierarchy
if let bannerView = banner {
view.addSubview(bannerView)
// 4. Setup Auto Layout
bannerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
bannerView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
bannerView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
bannerView.widthAnchor.constraint(equalToConstant: 320),
bannerView.heightAnchor.constraint(equalToConstant: 100)
])
}
// 5. Load ad
banner?.load()
}
deinit {
// 6. Remove banner before memory deallocation
banner?.removeFromSuperview()
banner = nil
}
}
// MARK: - AdropBannerDelegate
extension ViewController: AdropBannerDelegate {
func onAdReceived(_ banner: AdropBanner) {
print("Banner ad received successfully")
}
func onAdFailedToReceive(_ banner: AdropBanner, _ errorCode: AdropErrorCode) {
print("Banner ad failed to receive: \(errorCode)")
}
func onAdImpression(_ banner: AdropBanner) {
print("Banner ad impression")
}
func onAdClicked(_ banner: AdropBanner) {
print("Banner ad clicked")
}
}
AdropBanner Initialization
Unit ID created in Ad Control Console
let banner = AdropBanner(unitId: "YOUR_UNIT_ID")
Load Ad
Call the load() method to request an ad after adding the banner to the screen.
Call load() when the banner is visible on screen. Loading when not visible may result in inaccurate impression measurements.
Set Context ID
You can set a Context ID for contextual targeting.
// contextId is read-only — set via initializer
let banner = AdropBanner(unitId: "YOUR_UNIT_ID", contextId: "article_123")
SwiftUI Implementation
In SwiftUI environment, implement banner ads using AdropBannerRepresented.
Basic Implementation
import SwiftUI
import AdropAds
struct ContentView: View {
@State private var represented = AdropBannerRepresented(unitId: "YOUR_UNIT_ID")
var body: some View {
VStack {
Spacer()
Text("Main Content")
Spacer()
// Banner ad
represented
.frame(width: 320, height: 100)
.onAppear {
represented.banner.load()
}
}
}
}
Delegate Handling
You can handle ad events through delegates.
import SwiftUI
import AdropAds
struct ContentView: View {
@StateObject private var bannerDelegate = BannerDelegate()
@State private var represented = AdropBannerRepresented(unitId: "YOUR_UNIT_ID")
var body: some View {
VStack {
Spacer()
if bannerDelegate.isLoaded {
Text("Ad loaded successfully")
}
represented
.frame(width: 320, height: 100)
.onAppear {
represented.delegate = bannerDelegate
represented.banner.load()
}
}
}
}
// Delegate implementation
class BannerDelegate: NSObject, ObservableObject, AdropBannerDelegate {
@Published var isLoaded = false
func onAdReceived(_ banner: AdropBanner) {
print("Banner ad received successfully")
isLoaded = true
}
func onAdFailedToReceive(_ banner: AdropBanner, _ errorCode: AdropErrorCode) {
print("Banner ad failed to receive: \(errorCode)")
isLoaded = false
}
func onAdImpression(_ banner: AdropBanner) {
print("Banner ad impression")
}
func onAdClicked(_ banner: AdropBanner) {
print("Banner ad clicked")
}
}
Set Context ID
AdropBannerRepresented(
unitId: "YOUR_UNIT_ID",
contextId: "article_123"
)
.frame(width: 320, height: 100)
Objective-C Implementation
How to implement banner ads in Objective-C environment.
Basic Implementation
@import AdropAds;
@interface ViewController () <AdropBannerDelegate>
@property (nonatomic, strong) AdropBanner *banner;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 1. Create banner instance
self.banner = [[AdropBanner alloc] initWithUnitId:@"YOUR_UNIT_ID"];
// 2. Set delegate
self.banner.delegate = self;
// 3. Add to view hierarchy
[self.view addSubview:self.banner];
// 4. Setup Auto Layout
self.banner.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
[self.banner.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
[self.banner.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
[self.banner.widthAnchor constraintEqualToConstant:320],
[self.banner.heightAnchor constraintEqualToConstant:100]
]];
// 5. Load ad
[self.banner load];
}
- (void)dealloc {
[self.banner removeFromSuperview];
self.banner = nil;
}
#pragma mark - AdropBannerDelegate
- (void)onAdReceived:(AdropBanner *)banner {
NSLog(@"Banner ad received successfully");
}
- (void)onAdFailedToReceive:(AdropBanner *)banner :(AdropErrorCode)errorCode {
NSLog(@"Banner ad failed to receive: %ld", (long)errorCode);
}
- (void)onAdImpression:(AdropBanner *)banner {
NSLog(@"Banner ad impression");
}
- (void)onAdClicked:(AdropBanner *)banner {
NSLog(@"Banner ad clicked");
}
@end
Set Context ID
// contextId is read-only — set via initializer
AdropBanner *banner = [[AdropBanner alloc] initWithUnitId:@"YOUR_UNIT_ID" contextId:@"article_123"];
Delegate Methods
The AdropBannerDelegate protocol handles ad lifecycle events.
onAdReceived (Required)
Called when ad is received successfully.
func onAdReceived(_ banner: AdropBanner) {
print("Banner ad received successfully")
// Handle ad loading indicator, etc.
}
onAdFailedToReceive (Required)
Called when ad fails to load.
func onAdFailedToReceive(_ banner: AdropBanner, _ errorCode: AdropErrorCode) {
print("Banner ad failed to receive: \(errorCode)")
// Handle errors and display alternative content
}
Error code indicating the type of error. See Reference for details.
onAdImpression (Optional)
Called when ad is displayed on screen.
func onAdImpression(_ banner: AdropBanner) {
print("Banner ad impression")
// Handle impression analytics logging, etc.
}
onAdClicked (Optional)
Called when user clicks on the ad.
func onAdClicked(_ banner: AdropBanner) {
print("Banner ad clicked")
// Handle click analytics logging, etc.
}
Closure Callbacks
As an alternative to delegates, you can use closure-based callbacks.
let banner = AdropBanner(unitId: "YOUR_UNIT_ID")
banner.onAdReceived = { banner in
print("Banner ad received")
}
banner.onAdFailedToReceive = { banner, errorCode in
print("Banner ad failed: \(errorCode)")
}
banner.onAdImpression = { banner in
print("Banner ad impression")
}
banner.onAdClicked = { banner in
print("Banner ad clicked")
}
If both a delegate and closures are set, both will be called.
Ad Sizes
Banner ads require view size to match the size set in the unit.
Common Banner Sizes
| Size | Use Case |
|---|
| 320 x 50 | Small banner |
| 320 x 100 | Large banner |
| 16:9 ratio | Video banner |
UIKit
bannerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
bannerView.widthAnchor.constraint(equalToConstant: 320),
bannerView.heightAnchor.constraint(equalToConstant: 100)
])
SwiftUI
represented
.frame(width: 320, height: 100)
.onAppear {
represented.banner.load()
}
Best Practices
1. Memory Management
Remove the banner when the view controller is deallocated.
deinit {
banner?.removeFromSuperview()
banner = nil
}
2. Screen Visibility
Load ads when the banner is visible on screen.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
banner?.load()
}
3. Reuse
Call load() again to reuse the same banner instance.
func refreshAd() {
banner?.load()
}
4. Error Handling
Implement appropriate error handling when ad loading fails.
func onAdFailedToReceive(_ banner: AdropBanner, _ errorCode: AdropErrorCode) {
switch errorCode {
case .ERROR_CODE_NETWORK:
print("Network error: Check connection")
case .ERROR_CODE_AD_NO_FILL:
print("No available ads")
default:
print("Ad load failed: \(errorCode)")
}
// Hide ad area or display fallback content
bannerContainerView.isHidden = true
// Or show your own promotional banner
showPromotionBanner()
}
Test Unit IDs
Use the following test unit IDs during development and testing.
| Ad Type | Test Unit ID | Size |
|---|
| Banner (320x50) | PUBLIC_TEST_UNIT_ID_320_50 | 320 x 50 |
| Banner (320x100) | PUBLIC_TEST_UNIT_ID_320_100 | 320 x 100 |
| Carousel Banner | PUBLIC_TEST_UNIT_ID_CAROUSEL | Variable |
| Banner Video (16:9) | PUBLIC_TEST_UNIT_ID_BANNER_VIDEO_16_9 | 16:9 ratio |
| Banner Video (9:16) | PUBLIC_TEST_UNIT_ID_BANNER_VIDEO_9_16 | 9:16 ratio |
Usage Example
let banner = AdropBanner(unitId: AdropUnitId.PUBLIC_TEST_UNIT_ID_320_100)
Custom Click Handling
You can take control of ad click behavior by using useCustomClick. When enabled, the SDK will not automatically open the destination URL on click, and you can handle it yourself.
useCustomClick
let banner = AdropBanner(unitId: "YOUR_UNIT_ID")
banner.useCustomClick = true
banner.delegate = self
banner.load()
When useCustomClick is true, the SDK will not open the destination URL automatically when the ad is clicked. The onAdClicked delegate callback will still fire, and you can use the open() method or handle navigation yourself.
open()
Opens the destination URL of the ad. Can optionally pass a custom URL.
// Open the ad's default destination URL
banner?.open()
// Open a custom URL
banner?.open("https://example.com")
// Open using in-app browser
banner?.open(nil, useInAppBrowser: true)
Backfill Ads
When backfill ads are enabled, backfill ads are automatically loaded when direct ads are unavailable. Use the isBackfilled property to check if the ad is a backfill ad.
class ViewController: UIViewController, AdropBannerDelegate {
private var banner: AdropBanner?
func onAdReceived(_ banner: AdropBanner) {
if banner.isBackfilled {
print("Backfill ad loaded")
} else {
print("Direct ad loaded")
}
}
func onAdFailedToReceive(_ banner: AdropBanner, _ errorCode: AdropErrorCode) {
switch errorCode {
case .ERROR_CODE_AD_NO_FILL:
print("No direct ad, requesting backfill...")
case .ERROR_CODE_AD_BACKFILL_NO_FILL:
print("No backfill ad available")
// Hide ad area
banner.isHidden = true
default:
print("Ad load failed: \(errorCode)")
}
}
}
To use backfill ads, add the AdropAds-Backfill dependency. See Getting Started.
Next Steps
Native Ads
Implementing customizable native ads
Interstitial Ads
Implementing full-screen interstitial ads
Targeting Settings
Setting up user attributes and contextual targeting
Reference
Classes, delegates, error codes reference