Skip to main content

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

unitId
String
required
Unit ID created in AdControl 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.
banner?.load()
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.
banner?.contextId = "article_123"

SwiftUI Implementation

In SwiftUI environment, implement banner ads using AdropBannerRepresented.

Basic Implementation

import SwiftUI
import AdropAds

struct ContentView: View {
    var body: some View {
        VStack {
            Spacer()

            Text("Main Content")

            Spacer()

            // Banner ad
            AdropBannerRepresented(unitId: "YOUR_UNIT_ID")
                .frame(width: 320, height: 100)
        }
    }
}

Delegate Handling

You can handle ad events through delegates.
import SwiftUI
import AdropAds

struct ContentView: View {
    @StateObject private var bannerDelegate = BannerDelegate()

    var body: some View {
        VStack {
            Spacer()

            if bannerDelegate.isLoaded {
                Text("Ad loaded successfully")
            }

            AdropBannerRepresented(
                unitId: "YOUR_UNIT_ID",
                delegate: bannerDelegate
            )
            .frame(width: 320, height: 100)
        }
    }
}

// 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

self.banner.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
}
errorCode
AdropErrorCode
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.
}

Ad Sizes

Banner ads require view size to match the size set in the unit.

Common Banner Sizes

SizeUse Case
320 x 50Small banner
16:9 ratioVideo banner

UIKit

bannerView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
    bannerView.widthAnchor.constraint(equalToConstant: 320),
    bannerView.heightAnchor.constraint(equalToConstant: 100)
])

SwiftUI

AdropBannerRepresented(unitId: "YOUR_UNIT_ID")
    .frame(width: 320, height: 100)

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 .networkError:
        print("Network error: Check connection")
    case .noFill:
        print("No available ads")
    default:
        print("Ad load failed: \(errorCode)")
    }
}

Test Unit IDs

Use the following test unit IDs during development and testing.
Ad TypeTest Unit IDSize
Banner (320x50)PUBLIC_TEST_UNIT_ID_320_50320 x 50
Banner (320x100)PUBLIC_TEST_UNIT_ID_320_100320 x 100
Carousel BannerPUBLIC_TEST_UNIT_ID_CAROUSELVariable
Banner Video (16:9)PUBLIC_TEST_UNIT_ID_BANNER_VIDEO_16_916:9 ratio
Banner Video (9:16)PUBLIC_TEST_UNIT_ID_BANNER_VIDEO_9_169:16 ratio

Usage Example

let banner = AdropBanner(unitId: AdropUnitId.PUBLIC_TEST_UNIT_ID_320_100)

Next Steps