# Banner integration for iOS

> Implement banner ads in your iOS app by creating a banner ad object, setting its size, and displaying it within the app's view hierarchy.

Banners are rectangular, system-initiated ads that can be either static or animated, and are served in a designated area around your live app content.

> **Note:**
>
> This documentation is relevant for SDK 8.4.0+.

## Prerequisites

* Make sure that you have correctly integrated the LevelPlay SDK into your application. Integration is outlined [here](/grow/levelplay/sdk/ios/sdk-integration.md).
* Make sure to initialize the SDK using LevelPlay Initialization API.
* Find the AdUnitID in LevelPlay dashboard.

## Create Banner Ad Object and Set Size

The creation of the banner ad object should be done after receiving **onInitSuccess** callback.

1. **Objective-C**

   ```objective-c
   // Create ad configuration - optional
   LPMBannerAdViewConfigBuilder *adConfigBuilder = [LPMBannerAdViewConfigBuilder new];
   [adConfigBuilder setWithAdSize:bannerSize];
   [adConfigBuilder setWithPlacementName:@"placementName"];
   LPMBannerAdViewConfig *adConfig = [adConfigBuilder build];
   // Create the banner view and set the ad unit id
   self.bannerAd = [[LPMBannerAdView alloc] initWithAdUnitId:@"adUnitId" config: adConfig];
   ```

2. **Swift**

   ```swift
   // Create ad configuration - optional
   let adConfig = LPMBannerAdViewConfigBuilder()
   .set(adSize: bannerSize)
   .set(placementName: "placementName")
   .build()
   // Create the banner view and set the ad unit id
   self.bannerAd = LPMBannerAdView(adUnitId: "adUnitId", config: adConfig)
   ```

### Banner Sizes

| **LPMAdSize**       | Description                                                                   | Dimensions in dp (Width X Height) |
| ------------------- | ----------------------------------------------------------------------------- | --------------------------------- |
| bannerSize          | Standard banner                                                               | 320 x 50                          |
| largeSize           | Large banner                                                                  | 320 x 90                          |
| mediumRectangleSize | Medium Rectangular (MREC)                                                     | 300 x 250                         |
| Adaptive            | Automatically renders ads to adjust size and orientation for mobile & tablets | Device width X recommended height |

To create the ad size follow one of these options:

### Adaptive ad size that adjusts to the screen width (recommended)

This option returns BANNER or LEADERBOARD according to the device type. Networks that support the adaptive feature (Google, Yandex) will return a height based on their optimization logic.

1. **Objective-C**

   ```objective-c
   LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
   [self.bannerAdView setAdSize: bannerSize];
   ```

2. **Swift**

   ```swift
   let bannerSize = LPMAdSize.createAdaptive()
   self.bannerAdView.setAdSize(bannerSize)
   ```

### Specific banner size

This option allows you to set specifically a banner size: BANNER, LARGE, MEDIUM\_RECTANGLE.

1. **Objective-C**

   ```objective-c
   LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
   [self.bannerAdView setAdSize: bannerSize];
   ```

2. **Swift**

   ```swift
   let bannerSize = LPMAdSize.large()
   self.bannerAdView.setAdSize(bannerSize)
   ```

#### Placements

We support [placements](/grow/levelplay/platform/settings/placements.md) in banners for reporting only. Placements should be set before the **loadAd**, to affect all reloaded ads.

1. **Objective-C**

   ```objective-c
   // Set the placement name
   [self.bannerAdView setPlacementName:@"PlacementName"];
   ```

2. **Swift**

   ```swift
   // Set the placement name
   self.bannerAdView.setPlacementName("PlacementName")
   ```

## Set Banner Delegates

Implement the **LPMBannerAdViewDelegate** in your code to get informed of ad delivery.

* It is recommended to set the delegates before loading the banner ad.
* Each banner ad view should have its own delegate implementation.
* Callbacks run on the main thread.
* When callbacks are not supported by all networks, they are marked as optional.

1. **Objective-C**

   ```objective-c
   // Set delegate implementation 
   [self.bannerAdView setDelegate:self];
   #pragma mark - LPMBannerAdViewDelegate
   - (void)didLoadAdWithAdInfo:(nonnull LPMAdInfo *)adInfo {
   //Ad was loaded successfully
   }
   - (void)didFailToLoadAdWithAdUnitId:(nonnull NSString *)adUnitId error:(nonnull NSError *)error { 
   // Ad load failed
   }
   - (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {
   // Ad was clicked
   }
   - (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {
   // Ad was displayed and visible on screen
   }
   - (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {
   // Optional. Ad failed to be displayed on screen
   }
   - (void)didLeaveAppWithAdInfo:(LPMAdInfo *)adInfo {
   // Optional. User pressed on the ad and was navigated out of the app 
   }
   - (void)didExpandAdWithAdInfo:(LPMAdInfo *)adInfo {
   // Optional. Ad is opened on full screen
   }
   - (void)didCollapseAdWithAdInfo:(LPMAdInfo *)adInfo {
   // Optional. Ad is restored to its original size
   }
   ```

2. **Swift**

   ```swift
   // Set delegate implementation 
   self.bannerAdView.setDelegate(self)
   func didLoadAd(with adInfo: LPMAdInfo) {
   //Ad was loaded successfully 
   }
   func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {
   // Ad load failed
   }
   func didClickAd(with adInfo: LPMAdInfo) {
   // Ad was clicked
   }
   func didDisplayAd(with adInfo: LPMAdInfo) {
   // Ad was displayed and visible on screen
   }
   func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {
   // Optional. Ad failed to be displayed on screen
   }
   func didLeaveApp(with adInfo: LPMAdInfo) {
   // Optional. User pressed on the ad and was navigated out of the app 
   }
   func didExpandAd(with adInfo: LPMAdInfo) {
   // Optional. Ad is opened on full screen
   }
   func didCollapseAd(with adInfo: LPMAdInfo) {
   // Optional. Ad is restored to its original size
   }
   ```

### LevelPlay Ad Info

The **LPMAdInfo** parameter includes information about the loaded ad. Learn more about its implementation and available fields [here](/grow/levelplay/sdk/ios/levelplay-listener-adinfo-integration.md).

## Load Banner Ad

To load a banner ad, use **loadAd**.

1. **Objective-C**

   ```objective-c
   // Load the banner ad
   [self.bannerAdView loadAdWithViewController:self]
   ```

2. **Swift**

   ```swift
   // Load the banner ad
   self.bannerAdView.loadAd(with: self)
   ```

## **Pause and Resume Banner Refresh**

You can pause banner refresh in your code if the refresh value was defined in the platform. Use the following methods to stop the automatic refresh of the banner ad, or re-enable it after pausing.

> **Note:**
>
> When the banner is displayed again, it will complete the time until refresh from the time it was paused.

* **pauseAutoRefresh** - pauses auto-refresh of the banner ad.
* **resumeAutoRefresh** - resumes auto-refresh of the banner ad after it has been paused.

1. **Objective-C**

   ```objective-c
   // Pause refresh
   [self.bannerAdView pauseAutoRefresh]; 
   // Resume refresh
   [self.bannerAdView resumeAutoRefresh];
   ```

2. **Swift**

   ```swift
   // Pause refresh
   self.bannerAdView.pauseAutoRefresh()
   // Resume refresh
   self.bannerAdView.resumeAutoRefresh()
   ```

## **Destroy Banner Ad**

To destroy a banner, call the **destroy** method.

A destroyed banner can no longer be shown again. To display more ads, create a new **LPMBannerAdView** object.

1. **Objective-C**

   ```objective-c
   [self.bannerAdView destroy];
   ```

2. **Swift**

   ```swift
   self.bannerAdView.destroy()
   ```

## Full Implementation Example of Banner Ads

Here is an example for creating and loading a banner ad using adaptive banner size.

1. **Objective-C**

   ```objective-c
   NS_ASSUME_NONNULL_BEGIN
   @interface BannerAdViewController () <LPMBannerAdViewDelegate>
   @property(nonatomic, strong) LPMBannerAdView *bannerAd;
   @end
   @implementation BannerAdViewController
   - (void)dealloc {
   [self.bannerAd destroy];
   }
   - (void)createBannerAd {
   LPMAdSize *bannerSize = [self getBannerSize] ?: [LPMAdSize mediumRectangleSize];
   // Create ad configuration - optional
   LPMBannerAdViewConfigBuilder *adConfigBuilder = [LPMBannerAdViewConfigBuilder new];
   [adConfigBuilder setWithAdSize:bannerSize];
   [adConfigBuilder setWithPlacementName:@"placementName"];
   LPMBannerAdViewConfig *adConfig = [adConfigBuilder build];
   // Create the banner view and set the ad unit id
   self.bannerAd = [[LPMBannerAdView alloc] initWithAdUnitId:@"adUnitId" config: adConfig];

   // Set the delegate implementation
   [self.bannerAd setDelegate:self];

   // Add the banner view to the view hierarchy with the proper constraints
   [self addBannerViewWithSize:bannerSize];
   }
   - (LPMAdSize *)getBannerSize {
   // 1. Recommended - Adaptive ad size that adjusts to the screen width
   LPMAdSize *bannerSize = [LPMAdSize createAdaptiveAdSize];
   // 2. Adaptive ad size using fixed width ad size
   // self.bannerSize = [LPMAdSize createAdaptiveAdSizeWithWidth:400];
   // 3. Specific banner size - BANNER, LARGE, MEDIUM_RECTANGLE
   // self.bannerSize = [LPMAdSize mediumRectangleSize];

   return bannerSize;
   }
   - (void)loadBannerAd {
   [self.bannerAd loadAdWithViewController:self];
   }
   - (void)addBannerViewWithSize:(LPMAdSize *)bannerSize {
   self.bannerAd.translatesAutoresizingMaskIntoConstraints = NO;

   // Add the banner view to the view hierarchy
   [self.view addSubview:self.bannerAd];
   [NSLayoutConstraint activateConstraints:@[
      [self.bannerAd.bottomAnchor constraintEqualToAnchor:self.view.safeAreaLayoutGuide.bottomAnchor],
      [self.bannerAd.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor],
      [self.bannerAd.widthAnchor constraintEqualToConstant:bannerSize.width],
      [self.bannerAd.heightAnchor constraintEqualToConstant:bannerSize.height]
   ]];
   }
   #pragma mark - LPMBannerAdViewDelegate
   - (void)didLoadAdWithAdInfo:(nonnull LPMAdInfo *)adInfo {}
   - (void)didFailToLoadAdWithAdUnitId:(nonnull NSString *)adUnitId error:(nonnull NSError *)error {}
   - (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {}
   - (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {}
   - (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {}
   - (void)didLeaveAppWithAdInfo:(LPMAdInfo *)adInfo {}
   - (void)didExpandAdWithAdInfo:(LPMAdInfo *)adInfo {}
   - (void)didCollapseAdWithAdInfo:(LPMAdInfo *)adInfo {}
   @end
   NS_ASSUME_NONNULL_END
   ```

2. **Swift**

   ```swift
   import UIKit
   public class BannerAdViewController: UIViewController, LPMBannerAdViewDelegate {
   var bannerAd: LPMBannerAdView!
   deinit {
   self.bannerAd.destroy()
   }
   public func createBannerAd() {
   guard let bannerSize = getBannerSize() else {
      return
   }
   // Create ad configuration - optional
   let adConfig = LPMBannerAdViewConfigBuilder()
      .set(adSize: bannerSize)
      .set(placementName: "placementName")
      .build()
   // Create the banner view and set the ad unit id
   self.bannerAd = LPMBannerAdView(adUnitId: "adUnitId", config: adConfig)

   // Set the delegate implementation
   self.bannerAd.setDelegate(self)

   // Add the banner view to the view hierarchy with the proper constraints
   addBannerView(withSize: bannerSize)
   }
   public func getBannerSize() -> LPMAdSize? {
   // 1. Recommended - Adaptive ad size that adjusts to the screen width
   let bannerSize = LPMAdSize.createAdaptive()
   // 2. Adaptive ad size using fixed width ad size
   // bannerSize = LPMAdSize.createAdaptiveAdSize(withWidth: 400)
   // 3. Specific banner size - BANNER, LARGE, MEDIUM_RECTANGLE
   // bannerSize = LPMAdSize.mediumRectangleSize()

   return bannerSize
   }
   public func loadBannerAd() {
   self.bannerAd.loadAd(with: self)
   }
   public func addBannerView(withSize bannerSize: LPMAdSize) {
   DispatchQueue.main.async {
      self.bannerAd.translatesAutoresizingMaskIntoConstraints = false
      self.view.addSubview(self.bannerAd)
      let centerX = self.bannerAd.centerXAnchor.constraint(equalTo: self.view.centerXAnchor)
      let bottom = self.bannerAd.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor)
      let width = self.bannerAd.widthAnchor.constraint(equalToConstant: CGFloat(bannerSize.width))
      let height = self.bannerAd.heightAnchor.constraint(equalToConstant: CGFloat(bannerSize.height))
      NSLayoutConstraint.activate([centerX, bottom, width, height])
   }
   }
   // MARK: - LPMBannerAdViewDelegate
   public func didLoadAd(with adInfo: LPMAdInfo) {}
   public func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {}
   public func didClickAd(with adInfo: LPMAdInfo) {}
   public func didDisplayAd(with adInfo: LPMAdInfo) {}
   public func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {}
   public func didLeaveApp(with adInfo: LPMAdInfo) {}
   public func didExpandAd(with adInfo: LPMAdInfo) {}
   public func didCollapseAd(with adInfo: LPMAdInfo) {}
   }
   ```

## **LevelPlay Mediation Demo App**

The Integration Demo application demonstrates how to integrate banner Ad Unit APIs in your app.

[Download iOS Demo Application](https://github.com/ironsource-mobile/Mediation-Demo-Apps)

Verify your integration with our [Integration Test Suite](/grow/levelplay/sdk/ios/integration-test-suite.md).

## Next steps

Follow our integration guides to integrate additional Banner Ad networks or configure additional ad formats:

* [Mediation Networks](/grow/levelplay/sdk/ios/mediation-networks.md).
* [Rewarded Ads](/grow/levelplay/sdk/ios/rewarded-ads-integration.md)
* [Interstitial Ads](/grow/levelplay/sdk/ios/interstitial-integration.md)
* [Native Ads](/grow/levelplay/sdk/ios/native-ads-integration.md)
