SwiftUI

Integrating Banner Ads using SwiftUI

To load a banner ad, first create a UIViewRepresentable object, a wrapper that lets you integrate BIDBannerView, a UIKit view type object, into your SwiftUI view hierarchy.

import bidapp

struct ExampleSwiftUIWrapper: UIViewRepresentable
{
    func makeUIView(context: Context) -> MAAdView
    {
        let adView = BIDBannerView.banner(
            with: BIDAdFormat.banner_320x50 as! BIDAdFormat,
            delegate: bannerDelegate
        )
        adView.delegate = context.coordinator

        // Set background or background color for banners to be fully functional
        adView.backgroundColor = BACKGROUND_COLOR

        return adView
    }

    func updateUIView(_ uiView: MAAdView, context: Context) {}

    func makeCoordinator() -> Coordinator
    {
        Coordinator()
    }
}

Also, provide a custom Coordinator class for the wrapper object that conforms to BIDBannerViewDelegate so that you are notified of when your ad is ready and of other ad-related events. Inside the wrapper’s makeUIView method, create a BIDBannerView object.

To show that ad, add the UIViewRepresentable wrapper object inside your SwiftUI view hierarchy.

circle-info

You can find implementation examples in the GitHub repositoryarrow-up-right where the SwiftUI demo is integrated with existing Swift examples.

Last updated