Interstitial

Last Updated on: 19 Sep, 2023

Create Interstitial Ad

INFO

If you miss to Initialize the SDK, this method will fail to return a response.

To create a new interstitial ad, create a MesonInterstitial object using your adUnitId and call load method to make an ad request. This will request an ad from all demand sources, select a winner, and show it to the end user once the ad is available.

Parameters Data Type Description
adUnitId String

Present on Meson UI by creating an ad unit under an app. An ad unit id is a unique identifier.

For more information on how to set up an ad unit, see Set up Ad Units.

Swift

import Foundation
import MesonSDK

class InterstitialViewController: UIViewController {
    private var interstitial: MesonInterstitial!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        #warning("Please enter you adUnitId")
        /// Create an ad object
        interstitial = MesonInterstitial(adUnitId: "<Your ad unit id>", delegate: self)
        /// Request for the ad
        interstitial.load()
    }
} 

Objective-C

#import "InterstitialViewController.h"
@import MesonSDK;

@interface InterstitialViewController () <MesonInterstitialDelegate>
@property(nonatomic, strong) MesonInterstitial *interstitial;
@end

@implementation InterstitialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    #warning("Please enter you adUnitId")
    /// Create an ad object
    self.interstitial = [[MesonInterstitial alloc] initWithAdUnitId:@"<Your ad unit id>" delegate:self];
    /// Request for the ad
    [self.interstitial load];
} 

Delegates

Implement MesonInterstitialDelegate to get ad-related events.

Swift

extension InterstitialViewController: MesonInterstitialDelegate {
    /// Notifies the delegate that the interstitial has finished loading and can be shown instantly.
    func mesonInterstitialDidLoad(_ interstitial: MesonInterstitial) {
        print(#function)
        if self.interstitial.isReady {
            /// Show the Ad
            self.interstitial.show(from: self)
        }
    }

    /// Notifies the delegate that the interstitial has failed to load with some error.
    func mesonInterstitialDidFailToLoad(_ interstitial: MesonInterstitial, error: Error) {
        print(#function, error.localizedDescription)
    }

    /// Notifies the delegate that the interstitial has failed to be displayed with some error.
    func mesonInterstitialDidFailToDisplay(_ interstitial: MesonInterstitial, error: Error) {
        print(#function)
    }

    /// Notifies the delegate that the interstitial has been clicked.
    func mesonInterstitialDidClick(_ interstitial: MesonInterstitial, params: [String : Any]?) {
        print(#function)
    }

    /// Notifies the delegate that the user would be taken out of the application.
    func mesonInterstitialUserWillLeaveApplication(_ interstitial: MesonInterstitial) {
        print(#function)
    }

    /// Notifies the delegate that the interstitial would be displayed.
    func mesonInterstitialWillDisplay(_ interstitial: MesonInterstitial) {
        print(#function)
    }

    /// Notifies the delegate that the interstitial has been displayed.
    func mesonInterstitialDidDisplay(_ interstitial: MesonInterstitial) {
        print(#function)
    }

    /// Notifies the delegate that the interstitial will be dismissed.
    func mesonInterstitialWillDismiss(_ interstitial: MesonInterstitial) {
        print(#function)
    }

    /// Notifies the delegate that the interstitial has been dismissed.
    func mesonInterstitialDidDismiss(_ interstitial: MesonInterstitial) {
        print(#function)
    }

    /// Notifies the delegate that the interstitial has completed ad impression.
    func mesonInterstitialImpression(_ interstitial: MesonInterstitial, adData: MesonAdData?) {
        print(#function)
    }

    /// Notifies the delegate that the user has unlocked a reward for the action.
    func mesonRewardsUnlocked(_ interstitial: MesonInterstitial, rewards: [String : Any]) {
        print(#function)
    }
} 

Objective-C

#pragma mark - MesonInterstitialDelegate

/// Notifies the delegate that the interstitial has finished loading and can be shown instantly.
- (void)mesonInterstitialDidLoad:(MesonInterstitial * _Nonnull)interstitial {
    NSLog(@"%@", NSStringFromSelector(_cmd));
    
    if ([self.interstitial isReady]) {
        /// Show the Ad
        [self.interstitial showFromViewController:self];
    }
}

/// Notifies the delegate that the interstitial has failed to load with some error.
- (void)mesonInterstitialDidFailToLoad:(MesonInterstitial * _Nonnull)interstitial error:(NSError * _Nonnull)error{
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), error);
}

/// Notifies the delegate that the interstitial has failed to be displayed with some error.
- (void)mesonInterstitialDidFailToDisplay:(MesonInterstitial * _Nonnull)interstitial error:(NSError * _Nonnull)error {
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), error);
}

/// Notifies the delegate that the interstitial has been clicked.
- (void)mesonInterstitialDidClick:(MesonInterstitial * _Nonnull)interstitial params:(NSDictionary<NSString *, id> * _Nullable)params{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

/// Notifies the delegate that the user would be taken out of the application.
- (void)mesonInterstitialUserWillLeaveApplication:(MesonInterstitial * _Nonnull)interstitial{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

/// Notifies the delegate that the interstitial would be displayed.
- (void)mesonInterstitialWillDisplay:(MesonInterstitial * _Nonnull)interstitial{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

/// Notifies the delegate that the interstitial has been displayed.
- (void)mesonInterstitialDidDisplay:(MesonInterstitial * _Nonnull)interstitial{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

/// Notifies the delegate that the interstitial will be dismissed.
- (void)mesonInterstitialWillDismiss:(MesonInterstitial * _Nonnull)interstitial{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

/// Notifies the delegate that the interstitial has been dismissed.
- (void)mesonInterstitialDidDismiss:(MesonInterstitial * _Nonnull)interstitial{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

/// Notifies the delegate that the interstitial has completed ad impression.
- (void)mesonInterstitialImpression:(MesonInterstitial * _Nonnull)interstitial adData:(MesonAdData * _Nullable)adData{
    NSLog(@"%@", NSStringFromSelector(_cmd));
}

@end 

Show an Interstitial Ad

You can show an interstitial ad after the mesonInterstitialDidLoad callback. As mentioned above code snippet.

Call show on the mesonInterstitial ad instance.

Swift

if self.interstitial.isReady { 
       /// Show the Ad 
       self.interstitial.show(from: self) 
    } 

Objective-C

if ([self.interstitial isReady]) { 
        /// Show the Ad 
        [self.interstitial showFromViewController:self]; 
    }