Migrate to Rewarded Ad Unit API
This guide explains how to integrate the LevelPlay APIs available to use starting from SDK 8.5.0 (aka LevelPlay APIs), using an ad unit ID as a waterfall identifier, to load and display rewarded ads.
The advanced settings and regulations settings have not been changed, and they are supported before or after initializing the LevelPlay SDK.
Locate the ad unit ID in the LevelPlay platform
To load and display rewarded ads, you need to use the ad unit ID available in LevelPlay mediation platform:
In your LevelPlay account, navigate to Setup → Ad Units
Copy the rewarded ad unit ID and integrate it into your code
Initializing the ironSource SDK
To initialize the ironSource SDK, follow these steps:
- Define the completion handler success and failure.
- Define the list of ad formats to initialize in the session. This should include all the ad formats that you would like to use non multiple ad unit APIs.
- Call the LevelPlay init API using the appKey, ad formats, and user ID if relevant.
// Create a request builder with app key. Add User ID if available
LPMInitRequestBuilder *requestBuilder = [[LPMInitRequestBuilder alloc] initWithAppKey:@"appKey"];
[requestBuilder withUserId:@"UserId"];
// Build the initial request
LPMInitRequest *initRequest = [requestBuilder build];
// Initialize LevelPlay with the prepared request
[LevelPlay initWithRequest:initRequest completion:^(LPMConfiguration *_Nullable config, NSError *_Nullable error){
if(error) {
// There was an error on initialization. Take necessary actions or retry
} else {
// Initialization was successful. You can now load banner ad or perform other tasks
}
}];
// Create a request builder with app key. Add User ID if available
let requestBuilder = LPMInitRequestBuilder(appKey: "appKey")
.withUserId("UserId")
// Build the initial request
let initRequest = requestBuilder.build()
// Initialize LevelPlay with the prepared request
LevelPlay.initWith(initRequest)
{ config, error in
if let error = error {
// There was an error on initialization. Take necessary actions or retry
} else {
// Initialization was successful. You can now load ad or perform other tasks
}
}
Initialization Result
Success – triggered when the initialization is completed successfully. After you receive this indication, you can create and load the ad.
Error – the configuration was not retrieved successfully and ads cannot be loaded. It is recommended to try and initialize the ironSource SDK later (when internet connection is available, or when the failure reason is resolved)
Legacy | Ad Unit (new) | |
API | IronSource.initWithAppKey | LevelPlay.initWithRequest |
init result | onInitializationComplete | completion |
– | error |
Create Rewarded and Implement Delegates
Once you receive the completion result, you can create an ad unit using the relevant Ad Unit ID, as defined in the LevelPlay platform (Initializing the ironSource SDK step).
Implement the LPMRewardedAdDelegate for the rewarded ad unit created, to get informed of ad delivery.
self.rewardedAd = [[LPMRewardedAd alloc] initWithAdUnitId:@"adUnitId"];
self.rewardedAd.delegate = self;
#pragma mark - LPMRewardedAdDelegate Methods
- (void)didLoadAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToLoadAdWithAdUnitId:(NSString *)adUnitId error:(NSError *)error {}
- (void)didChangeAdInfo:(LPMAdInfo *)adInfo {}
- (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {}
- (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didCloseAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didRewardAdWithAdInfo:(LPMAdInfo *)adInfo reward:(LPMReward *)reward {}
self.rewardedAd = LPMRewardedAd(adUnitId: "adUnitId")
self.rewardedAd.setDelegate(self)
// MARK: LPMRewardedAdDelegate methods
func didLoadAd(with adInfo: LPMAdInfo) {}
func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {}
func didChangeAdInfo(_ adInfo: LPMAdInfo) {}
func didDisplayAd(with adInfo: LPMAdInfo) {}
func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {}
func didClickAd(with adInfo: LPMAdInfo) {}
func didCloseAd(with adInfo: LPMAdInfo) {}
func didRewardAd(with adInfo: LPMAdInfo, reward: LPMReward) {}
LevelPlay Rewarded Ad Events
didLoadAd – Provided when the ad is successfully loaded.
didFailToLoadAd – Provided when the ad fails to load. Ad Unit information is included.
didDisplayAd – Provided when the ad is displayed. This is equivalent to an impression.
didFailToDisplayAd – Provided when the ad fails to be displayed.
didRewardAd – Provided when the ad is rewarded. Ad Unit info and the reward info are included.
didClickAd (optional) – Provided when the user clicks on the ad.
didCloseAd – Provided when the ad is closed.
didChangeAdInfo (optional) – Provided when the ad info is updated. Available when another ad has loaded, and includes a higher CPM/Rate.
Legacy | Ad Unit (new) | |
Delegates | LevelPlayRewardedAdDelegate | LPMRewardedAdDelegate |
Events | onAdReady | didLoadAd |
onAdLoadFailed | didFailToLoadAd | |
onAdOpened | didDisplayAd | |
onAdClosed | didCloseAd | |
onAdShowFailed | didFailToDisplayAd | |
onAdRewarded | didRewardAd | |
onAdClicked | didClickAd | |
onAdShowSucceeded | – (deprecated) | |
– | didChangeAdInfo |
Load Rewarded Ad
Once you receive the onInitSuccess callback, you are ready to load a rewarded ad. This should be done using the method:
// Load or reload the ad
[self.rewardedAd loadAd];
// Load or reload the ad
self.rewardedAd.loadAd()
Show Rewarded Ad
You can show a rewarded ad after you receive onAdLoaded callback, using the showAd APIs.
If you are using placements, share their name as part of the API, as shown below.
// Show without placement
[self.rewardedAd showAdWithViewController:self placementName:nil];
// Show with placement
[self.rewardedAd showAdWithViewController:self placementName:placementName];
// Show without placement
self.rewardedAd.showAd(viewController: self, placementName: nil)
// Show with placement
self.rewardedAd.showAd(viewController: self, placementName: placementName)
Check Ad is Ready
To avoid show failures, and to make sure the ad could be displayed correctly, we recommend using the following API, before calling the showAd API.
isAdReady – returns true if ad was loaded successfully and ad unit is not capped, or false otherwise.
isPlacementCapped – returns true when a valid placement is capped. If the placement is not valid, or not capped, this API will return false.
// Check that ad is ready and that the placement is not capped
if ([self.rewardedAd isAdReady] && ![LPMRewardedAd isPlacementCapped:placementName]) {
[self.rewardedAd showAdWithViewController:self placementName:placementName];
}
// Check that ad is ready and that the placement is not capped
if self.rewardedAd.isAdReady(), !LPMRewardedAd.isPlacementCapped(placementName) {
self.rewardedAd.showAd(viewController: self, placementName: placementName)
}
Once the ad was displayed successfully to the player, you can load another ad, repeating the Load Rewarded Ad step. There is no need to create a new ad entity, when loading a single ad at a time.
Reward the User
The LevelPlay SDK will fire the onAdRewarded each time the user successfully completes a video.
The onAdRewarded and onAdClosed are asynchronous. Make sure to set up your listener to grant rewards even in cases where onAdRewarded is fired after the onAdClosed.
- (void)didRewardAdWithAdInfo:(LPMAdInfo *)adInfo reward:(LPMReward *)reward {
// Implement logic to grant the reward to the user
}
func didRewardAd(with adInfo: LPMAdInfo, reward: LPMReward) {
// Implement logic to grant the reward to the user
}
Multiple Ad Unit Rewarded APIs
Legacy | Ad Unit (new) | |
Class | IronSource | LPMRewardedAd |
API | loadRewardedVideo | loadAd |
showRewardedVideo | showAd | |
isRewardedVideoPlacementCapped | isPlacementCapped | |
isRewardedVideoAvailable | isAdReady | |
placement.getRewardName | reward.name | |
placement.getRewardAmount | reward.amount |
Full Implementation Example of Rewarded Ad
NS_ASSUME_NONNULL_BEGIN
@interface RewardedAdViewController () <LPMRewardedAdDelegate>
@property(nonatomic, strong) LPMRewardedAd *rewardedAd;
@end
@implementation RewardedAdViewController
- (void)createRewardedAd {
self.rewardedAd = [[LPMRewardedAd alloc] initWithAdUnitId:@"adUnitId"];
self.rewardedAd.delegate = self;
}
- (void)loadRewardedAd {
// Load or reload the ad
[self.rewardedAd loadAd];
}
- (void)showRewardedAd {
if ([self.rewardedAd isAdReady]) {
[self.rewardedAd showAdWithViewController:self placementName:NULL];
}
}
- (void)showRewardedAdWithPlacementName:(NSString *)placementName {
// Check that ad is ready and that the placement is not capped
if ([self.rewardedAd isAdReady] && ![LPMRewardedAd isPlacementCapped:placementName]) {
[self.rewardedAd showAdWithViewController:self
placementName:placementName];
}
}
#pragma mark - LPMRewardedAdDelegate Methods
- (void)didLoadAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToLoadAdWithAdUnitId:(NSString *)adUnitId error:(NSError *)error {}
- (void)didChangeAdInfo:(LPMAdInfo *)adInfo {}
- (void)didDisplayAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didFailToDisplayAdWithAdInfo:(LPMAdInfo *)adInfo error:(NSError *)error {}
- (void)didClickAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didCloseAdWithAdInfo:(LPMAdInfo *)adInfo {}
- (void)didRewardAdWithAdInfo:(LPMAdInfo *)adInfo reward:(LPMReward *)reward {}
@end
NS_ASSUME_NONNULL_END
class RewardedAdViewController: UIViewController, LPMRewardedAdDelegate {
var rewardedAd: LPMRewardedAd!
func createRewardedAd() {
self.rewardedAd = LPMRewardedAd(adUnitId: "adUnitId")
self.rewardedAd.setDelegate(self)
}
func loadRewardedAd() {
// Load or reload the ad
self.rewardedAd.loadAd()
}
func showRewardedAd() {
if self.rewardedAd.isAdReady() {
self.rewardedAd.showAd(viewController: self, placementName: nil)
}
}
func showRewardedAd(withPlacementName placementName: String) {
// Check that ad is ready and that the placement is not capped
if self.rewardedAd.isAdReady(), !LPMRewardedAd.isPlacementCapped(placementName) {
self.rewardedAd.showAd(viewController: self, placementName: placementName)
}
}
// MARK: LPMRewardedAdDelegate methods
func didLoadAd(with adInfo: LPMAdInfo) {}
func didFailToLoadAd(withAdUnitId adUnitId: String, error: Error) {}
func didChangeAdInfo(_ adInfo: LPMAdInfo) {}
func didDisplayAd(with adInfo: LPMAdInfo) {}
func didFailToDisplayAd(with adInfo: LPMAdInfo, error: Error) {}
func didClickAd(with adInfo: LPMAdInfo) {}
func didCloseAd(with adInfo: LPMAdInfo) {}
func didRewardAd(with adInfo: LPMAdInfo, reward: LPMReward) {}
}