# Banners

Banner ads are displayed in `RTBBannerView` instances, so the first step towards integrating banner ads is to create an instance of [`RTBBannerView`](https://ios-sdk-rtb.gravite.net/references/documentation/rtbsdk/rtbbannerview) and add it to your view hierarchy.

### Create an RTBBannerView Instance

```swift
let bannerAdView = RTBBannerView(size: .banner320x50)
```

You have to specify the [banner size](https://ios-sdk-rtb.gravite.net/references/documentation/rtbsdk/rtbbannersize) while initialising the `RTBBannerView`. It will be used later while loading a new banner ad.

### Listen to Callbacks

Through the use of [`RTBBannerViewDelegate`](https://ios-sdk-rtb.gravite.net/references/documentation/rtbsdk/rtbbannerviewdelegate), you can listen to the different callbacks. Of course, your class must conform to its delegate methods (see the [complete example below](#complete-code-example)).

```swift
bannerAdView.delegate = self
```

### Request Ad

To load a banner ad, you will need to pass an instance of [RTBBannerRequestConfiguration](https://ios-sdk-rtb.gravite.net/references/documentation/rtbsdk/rtbbannerrequestconfiguration) with `placementID`and `iTunesAppId`. Please contact our [support](mailto:support@gravite.net) for getting the needed IDs.

```swift
let configuration = RTBBannerRequestConfiguration(placementId: <PLACEMENT_ID>, iTunesAppId: <ITUNES_APP_ID>)
configuration.sellerId = "<SELLERID>" // Optional
configuration.bidFloor = <BID_FLOOR>  // Optional
bannerView.load(configuration: configuration)
```

You will be notified about the loading success/failure through the [`RTBBannerViewDelegate`](https://ios-sdk-rtb.gravite.net/references/documentation/rtbsdk/rtbbannerviewdelegate)&#x20;

> The currency of `bidFloor` and `priceCPM` parameters is USD.

### **Optional - set user targeting**

You can pass user targeting data to each request, allowing ads to be more relevant to your audience. Each field in [RTBUserTargeting](https://ios-sdk-rtb.gravite.net/references/documentation/rtbsdk/rtbusertargeting) is optional.

```swift
let configuration = RTBBannerRequestConfiguration(placementId: <PLACEMENT_ID>, iTunesAppId: <ITUNES_APP_ID>)
configuration.userTargeting = .init(userId: "<USER_ID>", gender: .male, yearOfBirth: 1999, keywords: ["keyword1", "keyword2"])
// yearOfBirth must be a 4-digit number, otherwise, it will be ignored
```

### Complete Code Example <a href="#complete-code-example" id="complete-code-example"></a>

```swift
import RTBSDK

class ViewController: UIViewController {
    let bannerAdView = RTBBannerView(size: <RTBBannerSize>)

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

    }
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        // [Important] add the banner view to your view hierarchy 
        self.view.addSubview(bannerAdView)
        
        // [Important] Set the banner ad delegate to listen to the callbacks
        bannerAdView.delegate = self
        
        // [Important] Add bannerView to your view hierarchy based on your screen layout
        ...
        // Prepare request configurations
        let configuration = RTBBannerRequestConfiguration(placementId: <PLACEMENT_ID>, iTunesAppId: <ITUNES_APP_ID>)
        configuration.sellerId = "<SELLERID>" // Optional
        configuration.bidFloor = <BID_FLOOR>  // Optional

        // Request a banner ad
        bannerAdView.load(configuration: configuration)
    }
}

extension ViewController: RTBBannerViewDelegate {
    func bannerViewDidReceiveAd(_ bannerView: RTBBannerView, bidInfo: RTBBidInfo, networkName: String) {
        // A banner ad has been loaded and displayed
    }

    func bannerView(_ bannerView: RTBBannerView, didFailToReceiveAd error: String, networkName: String) {
        // Failed to load a banner ad
    }

    func bannerViewDidRecordClick(_ bannerView: RTBBannerView, networkName: String) {
        // The banner received a click
    }

    func bannerViewDidPauseForAd(_ bannerView: RTBBannerView, networkName: String) {
        // The app paused after the use clicked on the ad and opened Safari app
    }

    func bannerViewDidResumeAfterAd(_ bannerView: RTBBannerView, networkName: String) {
        // The app resumed after returning from Safari as a result of user click
    }
}
```
