Skip to content

Banner Ads

Banner ads are rectangular ad units designed to fit within an app's layout. They remain visible as users engage with the app and can be positioned at the top, bottom, or inline with content. Banner ads support automatic refresh, allowing new ads to load at defined intervals.

Create BoldwinAdView

To create a banner ad, start by initializing a BoldwinAdView with the following parameters:

  • placementID - A string used to identify the ad placement
  • frame - The frame rectangle describing the view's location and size in its superview's coordinate system
  • adSizes - The desired sizes for the ad view
// Initialize BoldwinAdView
let adView = BoldwinAdView(
    placementID: placementID,
    frame: CGRect(origin: .zero, size: adSize),
    adSizes: [.BANNER_300x250]
)

// Add to UI
view.addSubview(adView)

Ad Sizes

A single ad unit can be configured to support multiple ad sizes:

adView.adSizes = [.BANNER_320x50, .BANNER_300x250, .init(width: 600, height: 100)]

Note: By setting this property you replace the ad sizes specified during initialization.

Auto Refresh

You can configure the ad refresh interval by setting autoRefreshPeriod to a positive number. If not specified, the default refresh interval is 60 seconds. The SDK enforces a minimum of 15 seconds and a maximum of 120 seconds, as intervals shorter than 15 seconds and intervals longer than 120 seconds may lead to a poor user experience.

Ad Events

You can optionally assign a delegate to receive callbacks for ad events:

adView.delegate = self
// MARK: - BoldwinAdViewDelegate
extension BoldwinBannerAdViewController: BoldwinAdViewDelegate {

    func adViewPresentationController() -> UIViewController? {
        // Specify controller for modal presentation.
        self
    }

    func adView(_ adView: BoldwinAdView, didReceiveAd: BoldwinAd) {
        // Called when ad is loaded.
    }

    func adView(_ adView: BoldwinAdView, didFailToReceiveAdWith error: BoldwinError) {
        // Called when Boldwin SDK failed to load ad.
        print("SDK did fail to receive ad with error: \(error.localizedDescription)")
    }

    func adViewWillPresentModal(_ adView: BoldwinAdView) {
        // Called when modal is about to be presented.
    }

    func adViewDidDismissModal(_ adView: BoldwinAdView) {
        // Called when modal is dismissed.
    }

    func adViewWillLeaveApplication(_ adView: BoldwinAdView) {
        // Called when the application is about to enter the background.
    }
}

You can also assign a delegate to receive notifications about ad size changes, allowing the app layout to be updated accordingly:

adView.adSizeDelegate = self
// MARK: - BoldwinAdViewSizeDelegate
extension BoldwinBannerAdViewController: BoldwinAdViewSizeDelegate {

    func adView(
        _ adView: BoldwinAdView,
        willChangeAdSizeTo size: BoldwinAdSize
    ) {
        // Called when ad view changes size.
        // Use to update the layout respectively.
    }
}

Ad Metadata

You can access the information about the loaded ad in adView(_:didReceiveAd:) delegate method. View the details here: BoldwinAd.

Load Ad

Once BoldwinAdView is configured, call load ad:

adView.loadAd()

See the complete integration example below:

import Boldwin

class BoldwinBannerAdViewController: UIViewController {

    private var boldwinAdView: BoldwinAdView?

    override func viewDidLoad() {
        super.viewDidLoad()
        createAd()
    }

    private func createAd() {
        // 1. Create BoldwinAdView instance.
        let adView = BoldwinAdView(
            placementID: "<YOUR_PLACEMENT_ID>",
            frame: CGRect(origin: .zero, size: CGSize(width: 300, height: 250)),
            adSizes: [
                .BANNER_300x250
            ]
        )

        // 2. Optionally set delegates.
        adView.delegate = self
        adView.adSizeDelegate = self

        // 3. Configure BoldwinAdView.
        adView.autoRefreshPeriod = 15

        // Store the instance.
        boldwinAdView = adView

        // Add to UI.
        containerView.addSubview(adView)

        // 5. Load ad.
        adView.loadAd()
    }
}

// MARK: - BoldwinAdViewDelegate
extension BoldwinBannerAdViewController: BoldwinAdViewDelegate {

    func adViewPresentationController() -> UIViewController? {
        // Specify controller for modal presentation.
        self
    }

    func adView(_ adView: BoldwinAdView, didReceiveAd: BoldwinAd) {
        // Called when ad is loaded.
    }

    func adView(_ adView: BoldwinAdView, didFailToReceiveAdWith error: BoldwinError) {
        // Called when Boldwin SDK failed to load ad.
        print("SDK did fail to receive ad with error: \(error.localizedDescription)")
    }

    func adViewWillPresentModal(_ adView: BoldwinAdView) {
        // Called when modal is about to be presented.
    }

    func adViewDidDismissModal(_ adView: BoldwinAdView) {
        // Called when modal is dismissed.
    }

    func adViewWillLeaveApplication(_ adView: BoldwinAdView) {
        // Called when the application is about to enter the background.
    }
}

// MARK: - BoldwinAdViewSizeDelegate
extension BoldwinBannerAdViewController: BoldwinAdViewSizeDelegate {

    func adView(
        _ adView: BoldwinAdView,
        willChangeAdSizeTo size: BoldwinAdSize
    ) {
        // Called when ad view changes size.
        // Use to update the layout respectively.
    }
}

SwiftUI integration example:

import SwiftUI
import Boldwin

struct BoldwinBannerViewRepresentable: UIViewRepresentable {

    let placementID: String
    let adSize: BoldwinAdSize

    func makeUIView(context: Context) -> BoldwinAdView {
        let adView = BoldwinAdView(
            placementID: placementID,
            frame: CGRect(origin: .zero, size: CGSize(width: adSize.width, height: adSize.height)),
            adSizes: [adSize]
        )
        adView.loadAd()
        return adView
    }

    func updateUIView(_ uiView: BoldwinAdView, context: Context) {
        // Update if needed
    }
}

// Usage in SwiftUI
struct ContentView: View {
    var body: some View {
        VStack {
            Spacer()

            BoldwinBannerViewRepresentable(
                placementID: "YOUR_BANNER_PLACEMENT_ID",
                adSize: .banner320x50
            )
            .frame(width: 320, height: 50)
        }
    }
}