Bidapp Ads
  • 🎁Ab_dout
  • Integration
    • iOS
      • Getting started
      • Additional Settings
      • App Policy Settings
      • Serverless Settings
      • Interstitial Ads
      • Rewarded Ads
      • Banner Ads
      • SKAdNetwork
      • SwiftUI
    • Android
      • Getting started
      • Additional Settings
      • App Policy Settings
      • Serverless Settings
      • Interstitial Ads
      • Rewarded Ads
      • Banner Ads
      • Jetpack Compose
  • PLUGINS
    • Kotlin Multiplatform
      • Getting started
      • Android Platform Setup
      • iOS Platfrom Setup
      • App Policy Settings
      • Interstitial Ads
      • Rewarded Ads
      • Banner Ads
    • Unreal Engine
      • Getting started
      • App Policy Settings
      • Interstitial Ads
      • Rewarded Ads
      • Banner Ads
      • MREC
    • Unity
      • Getting started
      • Android
      • iOS
      • App Policy Settings
      • Interstitial Ads
      • Rewarded Ads
      • Banner Ads
Powered by GitBook
On this page

Was this helpful?

  1. Integration
  2. iOS

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
            )
        }
    }
}
PreviousSKAdNetworkNextAndroid

Last updated 1 year ago

Was this helpful?

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

GitHub repository