Integrate Banner Ads

To integrate any ad format, you must first have completed the instructions in the basic integration article.

Overview

Banner ads do not occupy a full screen; instead, the publisher determines the location of the ad container within their app. The size of the banner container must be one of the four listed in Supported Banner Sizes. Banner ads can display anywhere you specify on the screen, and the user can continue using the app while the ad plays.

Supported Banner Sizes

Vungle supports the banner sizes in the table below. Specify the banner size when you create the VungleBanner instance by using one of the available enum values.

Banner Format Dimension
VungleAdSizeBannerRegular 320 x 50
VungleAdSizeBannerShort 300 x 50
VungleAdSizeMREC 300 x 250
VungleAdSizeBannerLeaderboard (tablet only) 728 x 90

Create a Banner Ad

  1. To integrate banner ads in your iOS app, open Main.storyboard, add a UIView to your ViewController, and name it bannerAdContainer.
    banner1.png
  2. In your ViewController.swift file (or ViewController.h file for Objective-C), import VungleAdsSDK and add an instance variable for VungleBannerView.
    SwiftObjective-C
    import UIKit
    import VungleAdsSDK
    class LOBannerViewController: UIViewController {
    @IBOutlet weak var bannerAdView: UIView!
    private var bannerAd: VungleBannerView?
  3. Add the following code to your method where you want to prepare the VungleBannerView instance. To create the VungleBannerView, you will need to provide the Placement ID and VungleAdSize (refer to the Supported Banner Sizes table above). The Placement ID you use should have been set up in the Monetize Dashboard to work with the type of banner to be displayed.

    Note that MREC ads need their own Placement ID. You cannot play an MREC ad in a Placement ID created for general banner ads. Separate Placement IDs are required for MREC ads and other types of banner ads.

    SwiftObjective-C
    self.bannerAd = VungleBannerView(placementId: <YOUR_PLACEMENT_ID>, vungleAdSize: VungleAdSize.VungleAdSizeBannerRegular)
    self.bannerAd?.delegate = self

Note: You must set your ViewController (or any class that declares conformance to VungleBannerViewDelegate) to the delegate property of the VungleBannerView instance (refer to the Register for Callbacks section below).

Load and Play a Banner Ad

  1. Once you create the VungleBannerView instance, you can load an ad by calling load().
    SwiftObjective-C
    // pass nil for waterfall ads. Else the bidPayload for the bidding ad.
    self.bannerAd?.load("bidpayload")
    // add the bannerAd instance as a subview to the view where you want to display the ad.
    self.bannerAdView.addSubview(self.bannerAd)
  2. Your ViewController (or any class that conforms to VungleBannerViewDelegate) will receive notification of load/play success or error through delegate callback methods (refer to the Register for Callbacks section below).

Dismiss a Banner Ad

When the banner playback is completed, we recommend cleaning up the VungleBannerView instance by removing it from the parent view. In the example below, the bannerAdView is the superView of the VungleBannerView instance:

SwiftObjective-C
for subView in self.bannerAdView.subviews {   
subView.removeFromSuperview()
}
self.bannerAd?.delegate = nil
self.bannerAd = nil

Register for Callbacks

To receive notifications for ad events, declare conformance to the VungleBannerViewDelegate protocol and implement callback methods. Each of the callback methods is optional, so you can implement only the methods you want.

SwiftObjective-C
extension LOBannerViewController: VungleBannerViewDelegate {
    // Ad load Events
    func bannerAdDidLoad(_ bannerView: VungleBannerView) {
        print("bannerAdDidLoad")
    }

    func bannerAdDidFail(_ bannerView: VungleBannerView, withError: NSError) {
        print("bannerAdDidFailToLoad/present")
    }
  
    func bannerAdWillPresent(_ bannerView: VungleBannerView) {
        print("bannerAdWillPresent")
    }
    
    func bannerAdDidPresent(_ bannerView: VungleBannerView) {
        print("bannerAdDidPresent")
    }
    
    func bannerAdDidTrackImpression(_ bannerView: VungleBannerView) {
        print("bannerAdDidTrackImpression")
    }
    
    func bannerAdDidClick(_ bannerView: VungleBannerView) {
        print("bannerAdDidClick")
    }
    
    func bannerAdWillLeaveApplication(_ bannerView: VungleBannerView) {
        print("bannerAdWillLeaveApplication")
    }
    
    func bannerAdWillClose(_ bannerView: VungleBannerView) {
        print("bannerAdWillClose")
    }
    
    func bannerAdDidClose(_ bannerView: VungleBannerView) {
        print("bannerAdDidClose")
    }
}

Test a Banner Ad

You can test a Banner ad in one of two ways: by setting your app status to Test Mode so that Vungle can deliver test ads to your app, or by adding a test device to your app so that Vungle can deliver test ads specifically to the test device. Follow the instructions in Test Your Integration: Test Mode and Test Devices.

Additional Resources

Questions?

Need further assistance, feel free to reach out to us, we’re here to help!

Was this article helpful?