Rewarded

Last Updated on: 19 Sep, 2023

Create Rewarded Ad

INFO

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

To create a new rewarded ad, create 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 the ad 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 RewardedViewController: UIViewController {
    private var rewarded: MesonInterstitial!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        #warning("Please enter you adUnitId")
        /// Create an ad object
        rewarded = MesonInterstitial(adUnitId: "<Your ad unit id>", delegate: self)
        /// Request for the ad
        rewarded.load()
    }
} 

Objective-C

#import "RewardedViewController.h"
@import MesonSDK;

@interface RewardedViewController () <MesonInterstitialDelegate>
@property(nonatomic, strong) MesonInterstitial *rewarded;
@end

@implementation RewardedViewController

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

Delegates

Implement MesonInterstitialDelegate to get ad-related events.

Swift

extension RewardedViewController: MesonInterstitialDelegate {
    /// Notifies the delegate that the interstitial has finished loading and can be shown instantly.
    func mesonInterstitialDidLoad(_ interstitial: MesonInterstitial) {
        print(#function)
        if self.rewarded.isReady {
            self.rewarded.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.rewarded isReady]) {
        /// Show the Ad
        [self.rewarded 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));
}

/// Notifies the delegate that the user has unlocked a reward for the action.
- (void)mesonRewardsUnlocked:(MesonInterstitial * _Nonnull)interstitial rewards:(NSDictionary<NSString *, id> * _Nonnull)rewards {
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), rewards);
}

@end

Show a Rewarded 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.rewarded.isReady { 
       self.rewarded.show(from: self) 
    } 

Objective-C

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

Rewarded Amount/Event

You can implement the mesonRewardsUnlocked event in MesonInterstitialDelegate. You can also check the implementation in Create Rewarded Ad section code snippet.

Swift

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

Objective-C

/// Notifies the delegate that the user has unlocked a reward for the action. 
- (void)mesonRewardsUnlocked:(MesonInterstitial * _Nonnull)interstitial rewards:(NSDictionary<NSString *, id> * _Nonnull)rewards { 
    NSLog(@"%@ %@", NSStringFromSelector(_cmd), rewards); 
}