Rewarded video ads are full-screen video ads that users have the option of watching in full in exchange for in-app rewards.
This guide shows you how to integrate rewarded video ads from Ad Manager into an iOS app.
Prerequisites
Request rewarded video
GADRewardBasedVideoAd has a singleton design, so the following example shows a request to load an ad being made to the shared instance:
Swift
GADRewardBasedVideoAd.sharedInstance().load(DFPRequest(), withAdUnitID: "/6499/example/rewarded-video")
Objective-C
[[GADRewardBasedVideoAd sharedInstance] loadRequest:[DFPRequest request] withAdUnitID:@"/6499/example/rewarded-video"];
To allow videos to be preloaded, we recommend making your load request call as
early as possible (for example, in your app delegate's
application:didFinishLaunchingWithOptions:
method).
Always test with test ads
When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.
The easiest way to load test ads is to use our dedicated test ad unit ID for iOS rewarded video:
/6499/example/rewarded-video
It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.
For more information about how the Mobile Ads SDK's test ads work, see Test Ads.
Set up event notifications
To set up event notification, insert the line shown in bold before your load request call:
Swift
GADRewardBasedVideoAd.sharedInstance().delegate = self GADRewardBasedVideoAd.sharedInstance().load(DFPRequest(), withAdUnitID: "/6499/example/rewarded-video")
Objective-C
[GADRewardBasedVideoAd sharedInstance].delegate = self; [[GADRewardBasedVideoAd sharedInstance] loadRequest:[DFPRequest request] withAdUnitID:@"/6499/example/rewarded-video"];
GADRewardBasedVideoAdDelegate
notifies you of rewarded video lifecycle events.
You are required to set the delegate prior to loading an ad. The most important
event in this delegate is
rewardBasedVideoAd:didRewardUserWithReward:
,
which is called when the user should be rewarded for watching a video. You may
optionally implement other methods in this delegate.
The following sample illustrates how to log each of the events available in
GADRewardBasedVideoAdDelegate:
.
Swift
func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didRewardUserWith reward: GADAdReward) { print("Reward received with currency: \(reward.type), amount \(reward.amount).") } func rewardBasedVideoAdDidReceive(_ rewardBasedVideoAd:GADRewardBasedVideoAd) { print("Reward based video ad is received.") } func rewardBasedVideoAdDidOpen(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Opened reward based video ad.") } func rewardBasedVideoAdDidStartPlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad started playing.") } func rewardBasedVideoAdDidCompletePlaying(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad has completed.") } func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad is closed.") } func rewardBasedVideoAdWillLeaveApplication(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { print("Reward based video ad will leave application.") } func rewardBasedVideoAd(_ rewardBasedVideoAd: GADRewardBasedVideoAd, didFailToLoadWithError error: Error) { print("Reward based video ad failed to load.") }
Objective-C
- (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd didRewardUserWithReward:(GADAdReward *)reward { NSString *rewardMessage = [NSString stringWithFormat:@"Reward received with currency %@ , amount %lf", reward.type, [reward.amount doubleValue]]; NSLog(rewardMessage); } - (void)rewardBasedVideoAdDidReceiveAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd { NSLog(@"Reward based video ad is received."); } - (void)rewardBasedVideoAdDidOpen:(GADRewardBasedVideoAd *)rewardBasedVideoAd { NSLog(@"Opened reward based video ad."); } - (void)rewardBasedVideoAdDidStartPlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd { NSLog(@"Reward based video ad started playing."); } - (void)rewardBasedVideoAdDidCompletePlaying:(GADRewardBasedVideoAd *)rewardBasedVideoAd { NSLog(@"Reward based video ad has completed."); } - (void)rewardBasedVideoAdDidClose:(GADRewardBasedVideoAd *)rewardBasedVideoAd { NSLog(@"Reward based video ad is closed."); } - (void)rewardBasedVideoAdWillLeaveApplication:(GADRewardBasedVideoAd *)rewardBasedVideoAd { NSLog(@"Reward based video ad will leave application."); } - (void)rewardBasedVideoAd:(GADRewardBasedVideoAd *)rewardBasedVideoAd didFailToLoadWithError:(NSError *)error { NSLog(@"Reward based video ad failed to load."); }
Display rewarded video
It is a best practice to ensure that a rewarded video ad has completed loading
before attempting to display it. The
isReady
method indicates that a rewarded video ad request has been successfully
fulfilled:
Swift
if GADRewardBasedVideoAd.sharedInstance().isReady == true { GADRewardBasedVideoAd.sharedInstance().present(fromRootViewController: self) }
Objective-C
if ([[GADRewardBasedVideoAd sharedInstance] isReady]) { [[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:self]; }
Reload a rewarded video
A handy place to load a new rewarded video ad after the previous one is in
rewardBasedVideoAdDidClose:
.
Swift
func rewardBasedVideoAdDidClose(_ rewardBasedVideoAd: GADRewardBasedVideoAd) { GADRewardBasedVideoAd.sharedInstance().load(DFPRequest(), withAdUnitID: "/6499/example/rewarded-video") }
Objective-C
- (void)rewardBasedVideoAdDidClose:(GADRewardBasedVideoAd *)rewardBasedVideoAd { [[GADRewardBasedVideoAd sharedInstance] loadRequest:[DFPRequest request] withAdUnitID:@"/6499/example/rewarded-video"]; }
Integration with Unity
For instructions on integrating rewarded video into a Unity project, see the Unity Rewarded Video guide.
Download the example
You can also download the rewarded video examples from GitHub to see rewarded video ads in action.