Banner 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 and Banner Video Ads.

For adding banner ads to your app, following are the steps:

  • Import the required headers
  • Define an ad slot
  • Set up delegates

Note:

The steps 1 and 2 listed below are same for both Banner Ads and Banner Video Ads integration. The difference in integration is in the 3rd step of handling delegates.

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

Use the below mentioned line to import the headers

Objective-C

@import MNetAdSdk;

Swift

import MNetAdSdk

Or, you can add the headers individually using the below listed lines

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

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

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

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

Objective-C

// Creating the Ad view and attaching it dynamically
MNetAdView *mnetAdView = [[MNetAdView alloc] init];
[mnetAdView setAdUnitId:DEMO_MN_AD_UNIT];
[mnetAdView setAdSize:MNetCreateAdSizeFromCGSize(kMNetBannerAdSize)];
[mnetAdView setRootViewController:self];
[mnetAdView setDelegate:self]; // Use [mnetAdView setVideoDelegate:self]; for Banner Video Ads

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

// Adding comma-separated keywords helps with context. This is optional
[mnetAdView setKeywords:@"lion, king, disney"];

// 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
[mnetAdView loadAd];

Swift

// Creating the Ad view and attaching it dynamically
let adView = MNetAdView()
adView.adUnitId = DemoConstants.DEMO_MN_AD_UNIT_320x50
adView.adSize = MNetCreateAdSizeFromCGSize(kMNetBannerAdSize)
adView.delegate = self
adView.rootViewController = self

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

// Adding comma-separated keywords helps with context. This is optional
adRequest.keywords = "lion, king, disney"

// 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
adView.loadAd()

[mnetAdView setRootViewController:self] is mandatory. When an ad is clicked, the link is displayed in a separate custom ViewController, presented on top of the RootViewController specified.

The setContextLink is an optional API on the adviews which allows the developers to provide a contextual link to the SDK.

NOTE: The above example illustrates the use of MNetAdView without multiple ad size.

You can also create a banner/video ads with multiple ad sizes. See below example.

Objective-C

MNetAdView *mnetAdView = [[MNetAdView alloc] init];
[mnetAdView setAdUnitId:DEMO_MN_AD_UNIT];
// set multiple ad sizes
[mnetAdView setAdSizes:[NSArray arrayWithObjects:MNetCreateAdSizeFromCGSize(kMNetBannerAdSize),
                                                         MNetCreateAdSizeFromCGSize(kMNetMediumAdSize), nil]];

[mnetAdView setRootViewController:self];

// Use [mnetAdView setVideoDelegate:self]; for Banner Video Ads
[mnetAdView setDelegate:self]; 

// Set this delegate to get ad size change callback
[mnetAdView setAdSizeDelegate:self]; 

[self.adView addSubview:mnetAdView];
[mnetAdView loadAd];

Swift

let adView = MNetAdView()
adView.adUnitId = DemoConstants.DEMO_MN_AD_UNIT_320x50

// set multiple ad sizes
adView.adSizes = [MNetCreateAdSizeFromCGSize(kMNetBannerAdSize), MNetCreateAdSizeFromCGSize(kMNetMediumAdSize)]

// Set this delegate to get ad size change callback
adView.adSizeDelegate = self

// Use adView.videoDelegate for Banner Video Ads
adView.delegate = self

adView.rootViewController = self
adView.keywords = "lion, king, disney"
adView.loadAd()

Step 3: Setting up Delegates

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

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

@optional

/// Callback when the ad is loaded
- (void)mnetAdDidLoad:(MNetAdView *)adView;

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

/// Callback when the ad is clicked
- (void)mnetAdDidClick:(MNetAdView *)adView;

You can listen to callbacks regarding ad size change by implementing the MNetAdViewSizeDelegate. Make sure to set the adSizeDelegate property of the MNetAdView object.

// Implement this method to listen for ad view size change callback to set ad view frame according to new size received
- (void)mnetAdView:(MNetAdView *)adView didChangeSize:(MNetAdSize *)size {
    CGSize adViewSize = MNetCGSizeFromAdSize(size);
    [adView setFrame:CGRectMake(0, 0, adViewSize.width, adViewSize.height)];
    // Additional code as per use case
}

Banner Video Ads Integration for iOS

MNetAdView can display video ads as well. To enable video ads in MNetAdView you will have to contact your Media.Net account manager. The 3rd step of setting up delegates for Banner Video Ads is as below.

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