# 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.&#x20;

```swift
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.

```swift
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.

```swift
// 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
            )
        }
    }
}
```

{% hint style="info" %}
You can find implementation examples in the [GitHub repository](https://github.com/bidapphub/bidapp-ads-ios) where the SwiftUI demo is integrated with existing Swift examples.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.bidapp.io/integration/ios/swiftui.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
