Banner Video Ads Integration for iOS

Do go through the steps in our Getting Started Guide to add Media.Net SDK to your project before starting the integration of Banner Video Ads.

Step 1: Import the required headers at the top of the file -

Objective-C

@import MNetAdSdk;

Swift

import MNetAdSdk

or you can add the headers individually -

#import <MNAdSdk/MNet.h>
#import <MNAdSdk/MNetAdView.h>
#import <MNAdSdk/MNetError.h>
#import <MNAdSdk/MNetAdRequest.h>
#import <MNetAdSdk/MNetAdSizeConstants.h>

Note:

The MNet.h contains all the SDK constants like ad dimensions.

The MNetAdRequest and MNetAdView are used for requesting the ad and rendering them onto a view.

Step 2: Defining an ad slot and loading an ad.

The integration of banner video ad to your app can be done as the below example.

Objective-C

// Creating the request object
MNetAdRequest *request = [MNetAdRequest newRequest];
// The keywords help in the context of the ads. These are optional.
[request setKeywords:@"coconut, peanut, cashewnut"];

// Creating the Ad view and attaching it dynamically
MNetAdView *mnetAdView = [[MNetAdView alloc]init];
[mnetAdView setAdUnitId:DEMO_MN_AD_UNIT];
[mnetAdView setSize:MNetCreateAdSizeFromCGSize(kMNetMediumAdSize)];
[mnetAdView setRootViewController:self];
[mnetAdView setVideoDelegate:self];

// Optionally, the context-link can also be provided
[mnetAdView setContextLink:@"http://my-context-link.com"];

// Adding the ad view to an existing ViewController (called customViewController)
CGRect adFrame = CGRectMake(0, 0, mnetAdView.size.width, mnetAdView.size.height);
[mnetAdView setFrame:adFrame];
[customViewController addSubview:mnetAdView];

// Loading the ad request
[mnetAdView loadAdForRequest:request];

Swift

// Creating the request object
let adRequest = MNetAdRequest.new()
// Adding comma-separated keywords helps with context. This is optional
adRequest.keywords = "lion, king, disney"

// Creating the Ad view and attaching it dynamically
let adView = MNetAdView.init()
adView.adUnitId = DemoConstants.DEMO_MN_AD_UNIT_320x50
adView.size = MNetCreateAdSizeFromCGSize(kMNetMediumAdSize)
adView.delegate = self
adView.videoDelegate = self
adView.rootViewController = self

// Optionally, the context-link can also be provided
adView.setContextLink("http://my-context-link.com")

// Adding the ad view to an existing ViewController (called customViewController)
self.customViewController.addSubview(adView)
// Setting the frame constraints using SnapKit. Use the equivalent in your library.
adView.snp.makeConstraints{(make)->Void in
    make.top.equalToSuperview()
    make.centerX.equalToSuperview()
    make.width.equalTo(width)
    make.height.equalTo(height)
}

// Loading the ad request
adView.loadAd(for: adRequest)

The [mnetAdView setContextLink:] is an optional API which allows the developers to provide a contextual link to the SDK.

NOTE: The above example illustrates the use of MNetAdRequest.

You can also create a banner-video ad without MNetAdRequest. See below example.

Objective-C

MNetAdView *mnetAdView = [[MNetAdView alloc]init];
[mnetAdView setAdUnitId:DEMO_MN_AD_UNIT_300x250_VIDEO];
[mnetAdView setSize:MNetCreateAdSizeFromCGSize(kMNetMediumAdSize)];
[mnetAdView setRootViewController:self];
[mnetAdView setVideoDelegate:self];

[self.adView addSubview:mnetAdView];

[mnetAdView loadAd];

Swift

let adView = MNetAdView.init()
adView.adUnitId = DemoConstants.DEMO_MN_AD_UNIT_320x50
adView.size = MNetCreateAdSizeFromCGSize(kMNetMediumAdSize)
adView.rootViewController = self
adView.keywords = "lion, king, disney"
adView.delegate = self // Use `adView.videoDelegate = self` for Banner Video Ads
adView.loadAd()

The MNetAdView itself contains all the methods required to create the MNetAdRequest object internally.

Step 3: Setting up Delegates

You can listen to callbacks regarding the video ad loading status by implementing the MNetVideoDelegate. Make sure to set the delegate property of the MNetAdView object.

MNetVideoDelegate contains the following important methods which you can listen to -

@optional

/// Callback when the video has loaded
- (void)mnetVideoDidLoad:(MNetAdView *)adView;

/// Callback when the video has started to play
- (void)mnetVideoDidStart:(MNetAdView *)adView;

/// Callback when then video has finished playing
- (void)mnetVideoDidComplete:(MNetAdView *)adView;

/// Callback when the ad has failed to load
- (void)mnetVideoDidFailToLoad:(MNetAdView *)adView withError:(MNetError *)error;

/// Callback when the video ad has been clicked
- (void)mnetVideoDidClick:(MNetAdView *)adView;