Interstitial 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 Interstitial Ads.

For adding interstitial 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 Interstitial Ads and Interstitial 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/MNetInterstitialAd.h>
#import <MNetAdSdk/MNetError.h>

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

To integrate an interstitial-ad, it is recommended that you have a non-local reference to the MNetInterstitialAd instance, so that it is not deallocated immediately after loadAd method is called. In the examples below, this is achieved by adding MNetInterstitialAd as a property to the sample-view-controller. Note that it would be suffice to make it an instance-variable as well.

The following code-block illustrates loading of an interstitial-ad.

Objective-C

// In the SampleViewController.h file
@interface SampleViewController : UIViewController<MNetInterstitialAdDelegate>
@property (nonatomic) MNetInterstitialAd *interstitialAd;
@end

// In the SampleViewController.m file
@implementation SampleViewController

/// Loads interstitial-ad
- (void)loadInterstitialAd{
    // DEMO_MN_AD_UNIT_ID is the ad unit id
    // Create an instance of MNetInterstitialAd
    self.interstitialAd = [[MNetInterstitialAd alloc] initWithAdUnitId:DEMO_MN_AD_UNIT_ID];

    // set interstitialDelegate for listening to events
    [interstitialAd setInterstitialDelegate:self]; // Use [interstitialAd setInterstitialVideoDelegate:self]; for Interstitial Video Ads

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

    // Load the ad
    [self.interstitialAd loadAd];
}
@end

Swift

class AdsDisplayViewController: UIViewController, MNetInterstitialAdDelegate{
    var interstitialAdView:MNetInterstitialAd?

    /// Loads interstitial-ad
    func loadInterstitialAd(){
        self.interstitialAdView = MNetInterstitialAd.init(adUnitId: DemoConstants.DEMO_INTERSTITIAL_AD_UNIT)

        // set interstitialDelegate for listening to events
        interstitialAdView.interstitialDelegate = self

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

        // Load the ad
        self.interstitialAdView.load()
    }
}

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

Note:

  • MNetInterstialAd works differently from Banner Ad, it does not directly display the ad as soon it is loaded. The method - showAdFromViewController: on the MNetInterstitialAd object needs to be called as soon as the ad is loaded to display it.

  • To display the ad, you need to use the MNetIntersitialDelegate protocol (MNetInterstitialVideoAdDelegate for Interstitial Video Ads) and implement the intersitialAdDidLoad (interstitialVideoAdDidLoad For Interstitial Video Ads) method. For example -

Objective-C

- (void)mnetInterstitialAdDidLoad:(MNetInterstitialAd *)interstitialAd {
    [interstitialAd showAdFromViewController:self];
}

Swift

func mnetInterstitialAdDidLoad(_ interstitialAd: MNetInterstitialAd) {
    interstitialAd.show(from: self)
}

Step 3: Setting up Delegates

You can listen to the following important delegates in MNetInterstialAdDelegate -

@optional
/// Callback when the ad has loaded
- (void)mnetInterstitialAdDidLoad:(MNetInterstitialAd *)interstitialAd;

/// Callback when the ad failed to load
- (void)mnetInterstitialAdDidFailToLoad:(MNetInterstitialAd *)interstitialAd withError:(MNetError *)error;

/// Callback when the ad is clicked
- (void)mnetInterstitialAdDidClick:(MNetInterstitialAd *)interstialAd;

/// Callback when the ad is shown
- (void)mnetInterstitialAdDidShow:(MNetInterstitialAd *)interstitialAd;

/// Callback when the ad is dismissed
- (void)mnetInterstitialAdDidDismiss:(MNetInterstitialAd *)interstitialAd;

Interstitial Video Ads Integration for iOS

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

You can listen to the following important delegates in MNetInterstitialVideoAdDelegate -

@optional

/// Callback when the video ad has loaded
- (void)mnetInterstitialVideoDidLoad:(MNetInterstitialAd *)interstitialAd;

/// Callback when the video ad failed to load
- (void)mnetInterstitialVideoDidFailToLoad:(MNetInterstitialAd *)interstitialAd withError:(MNetError *)error;

/// Callback when the video ad finished playing the video
- (void)mnetInterstitialVideoDidComplete:(MNetInterstitialAd *)interstitialAd;

/// Callback when the video starts playing
- (void)mnetInterstitialVideoDidStart:(MNetInterstitialAd *)interstitialAd;

/// Callback when the video ad is clicked
- (void)mnetInterstitialVideoDidClick:(MNetInterstitialAd *)interstitialAd;

/// Callback when the video ad is shown
- (void)mnetInterstitialVideoDidShow:(MNetInterstitialAd *)interstitialAd;

/// Callback when the interstitial ad is dismissed
- (void)mnetInterstitialVideoDidDismiss:(MNetInterstitialAd *)interstitialAd;