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.

extension ExampleSwiftUIWrapper
{
    class Coordinator: NSObject, BIDBannerViewDelegate
    {
        // MARK: BIDBannerViewDelegate Protocol

        func adView(_ adView: BIDBannerView, didLoadAd adInfo: BIDAdInfo) {
            print("[\(String(describing: adInfo.showSessionId))][\(String(describing: adInfo.waterfallId))] readyToRefreshBanner: \(adInfo.networkId) [\(adInfo)]")
        }
    
        func adView(_ adView: BIDBannerView, didDisplayAd adInfo: BIDAdInfo) {
            print("[\(String(describing: adInfo.showSessionId))][\(String(describing: adInfo.waterfallId))] didDisplayBanner: \(adInfo.networkId) [\(adInfo)]")
        }
    
        func adView(_ adView: BIDBannerView, didFailToDisplayAd adInfo: BIDAdInfo, error: Error) {
            print("[\(String(describing: adInfo.showSessionId))][\(String(describing: adInfo.waterfallId))] didFailToDisplayBanner: \(adInfo.networkId) [\(adInfo)] ERROR: \(error.localizedDescription)")
        }
    
        func adView(_ adView: BIDBannerView, didClicked adInfo: BIDAdInfo) {
            print("[\(String(describing: adInfo.showSessionId))][\(String(describing: adInfo.waterfallId))] bannerDidClicked: \(adInfo.networkId) [\(adInfo)]")
        }
    }
}

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

// SwiftUI view to show ad
struct ExampleSwiftUIBannerAdView: View
{
    var body: some View {
        
        VStack(alignment: .center) {
            Spacer()
            Text("Hello bidapp!")
            Spacer()
        }.safeAreaInset(edge: .bottom) {
            ExampleSwiftUIWrapper().frame(
                minWidth: 320,
                maxWidth: 320,
                minHeight: 50,
                maxHeight: 50,
                alignment: .bottom
            )
        }
    }
}

You can find implementation examples in the GitHub repository where the SwiftUI demo is integrated with existing Swift examples.

Last updated