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
- To integrate banner ads in your iOS app, open
Main.storyboard, add aUIViewto yourViewController, and name itbannerAdContainer. - In your
ViewController.swiftfile (orViewController.hfile for Objective-C), importVungleAdsSDKand add an instance variable forVungleBannerView.
import UIKit
import VungleAdsSDK
class LOBannerViewController: UIViewController {
@IBOutlet weak var bannerAdView: UIView!
private var bannerAd: VungleBannerView?#import "LOBannerViewController.h"
#import <VungleAdsSDK/VungleAdsSDK.h>
@interface LOBannerViewController () <VungleBannerViewDelegate>
@property (nonatomic, weak) IBOutlet UIView *bannerAdView;
@property (nonatomic, strong) VungleBannerView *bannerAd;
@end - Add the following code to your method where you want to prepare the
VungleBannerViewinstance. To create theVungleBannerView, 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.
self.bannerAd = VungleBannerView(placementId: <YOUR_PLACEMENT_ID>, vungleAdSize: VungleAdSize.VungleAdSizeBannerRegular)
self.bannerAd?.delegate = selfself.bannerAd = [[VungleBannerView alloc] initWithPlacementId:<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
- Once you create the
VungleBannerViewinstance, you can load an ad by callingload().
// 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)// 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]; - Your
ViewController(or any class that conforms toVungleBannerViewDelegate) 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:
for subView in self.bannerAdView.subviews {
subView.removeFromSuperview()
}
self.bannerAd?.delegate = nil
self.bannerAd = nil
for (id 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.
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")
}
}
#pragma mark - VungleBannerViewDelegate Methods
- (void)bannerAdDidLoad:(VungleBannerView *)bannerView {
NSLog(@"bannerAdDidLoad");
}
- (void)bannerAdDidFail:(VungleBannerView *)bannerView
withError:(NSError *)withError {
NSLog(@"bannerAdDidFailToLoad");
}
- (void)bannerAdWillPresent:(VungleBannerView *)bannerView {
NSLog(@"bannerAdWillPresent");
}
- (void)bannerAdDidPresent:(VungleBannerView *)bannerView {
NSLog(@"bannerAdDidPresent");
}
- (void)bannerAdDidTrackImpression:(VungleBannerView *)bannerView {
NSLog(@"bannerAdDidTrackImpression");
}
- (void)bannerAdDidClick:(VungleBannerView *)bannerView {
NSLog(@"bannerAdDidClick");
}
- (void)bannerAdWillLeaveApplication:(VungleBannerView *)bannerView {
NSLog(@"bannerAdWillLeaveApplication");
}
- (void)bannerAdWillClose:(VungleBannerView *)bannerView {
NSLog(@"bannerAdWillClose");
}
- (void)bannerAdDidClose:(VungleBannerView *)bannerView {
NSLog(@"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
- Refer to our Banner ad examples in Swift or Objective-C.
- Refer to our MREC ad examples in Swift or Objective-C.