Rewarded ads integration 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 10 minutes

The Unity LevelPlay Rewarded is a full-screen ad unit, usually served at natural transition points during the app lifecycle. 

Prerequisites

  • Ensure that you have correctly integrated the ironSource SDK into your application. Integration is outlined here.
  • Ensure that you initialize the SDK using LevelPlay Initialization API.
  • Find the AdUnitID in LevelPlay dashboard.

Create Rewarded Ad Object

The creation of the Rewarded ad object must be performed after receiving the onInitSuccess callback.

The object is a reusable instance that can handle multiple loads and shows throughout the session. After creation, it should be used to load and show ads for the same ad unit.

For more advanced implementations, you can create multiple Rewarded ad objects if necessary.

self.rewardedAd = [[LPMRewardedAd alloc] initWithAdUnitId:@"adUnitId"];	 ```
</Tab>
<Tab title="Swift">
    ```java
self.rewardedAd = LPMRewardedAd(adUnitId: "adUnitId")	 ```
</Tab>
</Tabs>

## Implement Delegates

Implement the **LPMRewardedAdDelegate** in your code to get informed of ad delivery. 

- It is recommended to set the delegate before loading the Rewarded ad.
- Each Rewarded ad should have its own delegate implementation.
- Delegate methods run on the main thread.

<Tabs>
<Tab title="Objective-C">
```objective-c
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 Ad Info

The LPMAdInfo parameter includes information about the loaded ad.

Load Rewarded Ad

To load a Rewarded ad use loadAd.

[self.rewardedAd loadAd];
self.rewardedAd.loadAd()	 

Show Rewarded Ad

Show a Rewarded ad after you receive the didLoadAd callback.

  • It is required to share ViewController
  • If using placements, pass the placement name in the showAd API as shown in the Placements section below.
  • After the ad has been successfully displayed to the user, you can load another ad by repeating the loading step. 
- (void)showRewardedAd {
// Check that ad is ready
if ([self.rewardedAd isAdReady]) {
   // Show without placement
   [self.rewardedAd showAdWithViewController:self placementName:NULL];
}
}	 ```
</Tab>
<Tab title="Swift">
    ```java
func showRewardedAd() {
// Check that ad is ready  
if self.rewardedAd.isAdReady() {
   // Show without placement
   self.rewardedAd.showAd(viewController: self, placementName: nil)
}
}	 ```
</Tab>
</Tabs>

#### 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.

<Tabs>
<Tab title="Objective-C">
```objective-c
// 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)
}	 ```
</Tab>
</Tabs>

#### Placements

We support [placements](alex://grow-levelplay-platform-settings-placements) pacing and capping for Rewarded on the LevelPlay dashboard. 

If placements are set up for Rewarded ads, call the **showAd** method to serve the ad for a specific placement.

<Tabs>
<Tab title="Objective-C">
```objectivec
// Check that ad is ready and that the placement is not capped
if ([self.rewardedAd isAdReady] && ![LPMRewardedAd isPlacementCapped:placementName]) {
// Show ad with placement
[self.rewardedAd showAdWithViewController:self placementName:@"PlacementName"];
}	 ```
</Tab>
<Tab title="Swift">
```java
// Check that ad is ready and that the placement is not capped 
if self.rewardedAd.isAdReady(), !LPMRewardedAd.isPlacementCapped(placementName) {
// Show ad with placement
self.rewardedAd.showAd(viewController: self, placementName: placementName) 
}	 ```
</Tab>
</Tabs>

## 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**.

<Tabs>
<Tab title="Objective-C">
```objective-c
- (void)didRewardAdWithAdInfo:(LPMAdInfo *)adInfo reward:(LPMReward *)reward {
// Implement logic to grant the reward to the user
NSString *name = reward.name; 
NSInteger *amount = reward.amount;
}
func didRewardAd(with adInfo: LPMAdInfo, reward: LPMReward) {
// Implement logic to grant the reward to the user
let name: String = reward.name 
let amount: Int = reward.amount
}

Full Implementation Example of Rewarded Ads

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 {
// used to 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() {
// used to 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) {} 
}

LevelPlay Mediation Demo App

The Integration Demo application demonstrates how to integrate Rewarded Ad Unit APIs in your app.

Download iOS Demo Application

Verify your integration with our Integration Test Suite.

Next steps

Follow our integration guides to integrate additional Rewarded Ad networks or configure additional ad formats: