Migrate to rewarded ad unit API for iOS
Implement the Rewarded Video ad unit in your iOS app by initializing the ad unit, setting delegates, and handling ad events.
Read time 5 minutesLast updated 4 hours ago
This guide explains how to integrate the LevelPlay APIs available to use starting from SDK 8.6.0 (aka LevelPlay APIs), using an ad unit ID as a waterfall identifier, to load and display rewarded ads.
Prerequisites
APIs introduced in this article should be used instead of the integration methods currently in use (ironSource init, ironSource load rewarded, rewarded listeners). You can find the API replacements below. 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 codeInitializing 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 } }];
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)Component | Legacy | Ad Unit (new) |
---|---|---|
API | IronSource.initWithAppKey | LevelPlay.initWithRequest |
init result | onInitializationComplete | completion |
– | error |
Create Rewarded and
The creation of the rewarded ad object must be performed after receiving the completion result. The object is a reusable instance that can handle multiple loads and shows throughout the session. Once created, it should be used to load and show ads for the same ad unit. For more advanced implementations, you may create multiple rewarded ad objects if necessary.// Create rewarded ad self.rewardedAd = [[LPMRewardedAd alloc] initWithAdUnitId:@"adUnitId"];
Implement Rewarded Delegate
Implement the LPMRewardedAdDelegate for the rewarded ad unit created, to get informed of ad delivery.- It is recommended to set the listener before loading the rewarded ad.
- Each rewarded ad should have its own listener implementation.
- Callbacks run on the main thread.
// Create rewarded ad self.rewardedAd = [[LPMRewardedAd alloc] initWithAdUnitId:@"adUnitId"]; // Implement delegate 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 {}
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.Component | 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];
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];
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]; }
Reward the User
The LevelPlay SDK will fire the didRewardAd each time the user successfully completes a video. The didRewardAd and didCloseAd are asynchronous. Make sure to set up your listener to grant rewards even in cases where didRewardAd is fired after the didCloseAd.- (void)didRewardAdWithAdInfo:(LPMAdInfo *)adInfo reward:(LPMReward *)reward { // Implement logic to grant the reward to the user NSString *name = reward.name; NSInteger *amount = reward.amount; }
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