To integrate any ad format, you must first have completed the instructions in the basic integration article.
Rewarded ads are a full screen experience where users opt in to view a video ad in exchange for something of value, such as virtual currency, in-app items, exclusive content, and more. The ad experience grants the reward after the user has watched the video for a specified duration. When that duration has been reached, your app receives a callback to grant the reward to the user.
Create a Rewarded Ad
- To integrate rewarded ads in your iOS app, import
VungleAdsSDK
and add an instance variable forVungleRewarded
in yourViewController.swift
file (orViewController.h
file for Objective-C).import UIKit import VungleAdsSDK class LORewardedViewController: UIViewController { private var rewardedAd: VungleRewarded?
#import "LORewardedViewController.h" #import <VungleAdsSDK/VungleAdsSDK.h> @interface LORewardedViewController () <VungleRewardedDelegate> @property (nonatomic, strong) VungleRewarded *rewardedAd; @end
- Add the following code to your method where you want to prepare the
VungleRewarded
instance. To create theVungleRewarded
, you will need a Placement ID.self.rewardedAd = VungleRewarded(placementId: <YOUR_PLACEMENT_ID>) self.rewardedAd?.delegate = self
self.rewardedAd = [[VungleRewarded alloc] initWithPlacementId:<YOUR_PLACEMENT_ID>]; self.rewardedAd.delegate = self;
Note: You must set your ViewController
(or any class that declares conformance to VungleRewardedDelegate
) to the delegate property of the VungleRewarded
instance (refer to the Register for Callbacks section below).
Load a Rewarded Ad
- Once you create the
VungleRewarded
instance, you can load an ad by callingload()
.self.rewardedAd = VungleRewarded(placementId: <YOUR_PLACEMENT_ID>) self.rewardedAd?.delegate = self self.rewardedAd?.load()
self.rewardedAd = [[VungleRewarded alloc] initWithPlacementId:<YOUR_PLACEMENT_ID>]; self.rewardedAd.delegate = self; [self.rewardedAd load:nil];
- Your
ViewController
(or any class that conforms toVungleRewardedDelegate)
will receive notification of load success or error through delegate callback methods (refer to the Register for Callbacks section below).
Play a Rewarded Ad
- Once the rewarded ad is successfully loaded, you can display it by calling
present(with: <YOUR_ViewController>)
.self.rewardedAd?.present(with: <YOUR_ViewController>)
[self.rewardedAd presentWith:<YOUR_ViewController>];
- During the time the rewarded ad is visible to the user, you can listen for ad lifecycle events through delegate callback methods (refer to the Register for Callbacks section below).
- The ad is closed when the user closes it. You do not need to add steps in your code to close it.
Register for Callbacks
To receive notifications for ad events, declare conformance to VungleRewardedDelegate protocol, and implement callback methods. Each of the callback methods is optional, so you only need to implement the methods you want.
extension LORewardedViewController: VungleRewardedDelegate { // Ad load events func rewardedAdDidLoad(_ rewarded: VungleRewarded) { print("rewardedAdDidLoad") } func rewardedAdDidFailToLoad(_ rewarded: VungleRewarded, withError: NSError) { print("rewardedAdDidFailToLoad") } // Ad Lifecycle Events func rewardedAdWillPresent(_ rewarded: VungleRewarded) { print("rewardedAdWillPresent") } func rewardedAdDidPresent(_ rewarded: VungleRewarded) { print("rewardedAdDidPresent") } func rewardedAdDidFailToPresent(_ rewarded: VungleRewarded, withError: NSError) { print("rewardedAdDidFailToPresent") } func rewardedAdDidTrackImpression(_ rewarded: VungleRewarded) { print("rewardedAdDidTrackImpression") } func rewardedAdDidClick(_ rewarded: VungleRewarded) { print("rewardedAdDidClick") } func rewardedAdWillLeaveApplication(_ rewarded: VungleRewarded) { print("rewardedAdWillLeaveApplication") } func rewardedAdDidRewardUser(_ rewarded: VungleRewarded) { print("rewardedAdDidRewardUser") } func rewardedAdWillClose(_ rewarded: VungleRewarded) { print("rewardedAdWillClose") } func rewardedAdDidClose(_ rewarded: VungleRewarded) { print("rewardedAdDidClose") } }
#pragma mark - VungleRewarded Delegate Methods // Ad load events - (void)rewardedAdDidLoad:(VungleRewarded *)rewarded { NSLog(@"rewardedAdDidLoad"); } - (void)rewardedAdDidFailToLoad:(VungleRewarded *)rewarded withError:(NSError *)withError { NSLog(@"rewardedAdDidFailToLoad"); } // Ad Lifecycle Events - (void)rewardedAdWillPresent:(VungleRewarded *)rewarded { NSLog(@"rewardedAdWillPresent"); } - (void)rewardedAdDidPresent:(VungleRewarded *)rewarded { NSLog(@"rewardedAdDidPresent"); } - (void)rewardedAdDidFailToPresent:(VungleRewarded *)rewarded withError:(NSError *)withError { NSLog(@"rewardedAdDidFailToPresent"); } - (void)rewardedAdDidTrackImpression:(VungleRewarded *)rewarded { NSLog(@"rewardedAdDidTrackImpression"); } - (void)rewardedAdDidClick:(VungleRewarded *)rewarded { NSLog(@"rewardedAdDidClick"); } - (void)rewardedAdWillLeaveApplication:(VungleRewarded *)rewarded { NSLog(@"rewardedAdWillLeaveApplication"); } - (void)rewardedAdDidRewardUser:(VungleRewarded *)rewarded { NSLog(@"rewardedAdDidRewardUser"); } - (void)rewardedAdWillClose:(VungleRewarded *)rewarded { NSLog(@"rewardedAdWillClose"); } - (void)rewardedAdDidClose:(VungleRewarded *)rewarded { NSLog(@"rewardedAdDidClose"); }
Test a Rewarded Ad
You can test a rewarded ad in one of two ways: by setting your app status to Test Mode so that Vungle can deliver test ads to your app, or by adding a test device to your app so that Vungle can deliver test ads specifically to the test device. Follow the instructions in Test Your Integration: Test Mode and Test Devices.
Additional Resources
- Refer to our Rewarded ad examples in Swift or Objective-C.
- Refer to our example of setting customized text for Rewarded ad alerts in Swift or Objective-C.