Rewarded ads are ads that users have the option of interacting with in exchange for in-app rewards. This guide shows you how to integrate rewarded ads from Ad Manager into an iOS app.
Prerequisites
Google Mobile Ads SDK 7.42.2 or higher.
Create a rewarded ad object
Rewarded ads are requested and shown by
GADRewardedAd
objects. The first step in using one is to instantiate it and set its ad unit ID. For example, here's how to create aGADRewardedAd
in theviewDidLoad:
method of aUIViewController
:@import GoogleMobileAds; @import UIKit; @interface ViewController () @property(nonatomic, strong) GADRewardedAd *rewardedAd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:@"/6499/example/rewarded-video"]; }
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 ads:
/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.
Load an ad
To load a rewarded ad, call
loadRequest:completionHandler:
on aGADRewardedAd
object:@import GoogleMobileAds; @import UIKit; @interface ViewController () @property(nonatomic, strong) GADRewardedAd *rewardedAd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:@"/6499/example/rewarded-video"]; DFPRequest *request = [DFPRequest request]; [self.rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) { if (error) { // Handle ad failed to load case. } else { // Ad successfully loaded. } }]; }
This code loads a rewarded ad with the provided
DFPRequest
and completion block. Upon successful completion of the ad request, the completion block will be executed with a nilGADRequestError
parameter. If ad load failed, the non-nil error object provides failure information.Show the ad
Before displaying a rewarded ad to users, you must present the user with an explicit choice to view rewarded ad content in exchange for a reward. Rewarded ads must always be an opt-in experience.
To show a rewarded ad, check the
isReady
property onGADRewardedAd
to verify that it's finished loading, then callpresentFromRootViewController:delegate:
. Here's an example of how to do this in one of the action methods in aUIViewController
:@import GoogleMobileAds; @import UIKit; @interface ViewController () <GADRewardedAdDelegate> @property(nonatomic, strong) GADRewardedAd *rewardedAd; @end @implementation ViewController - (IBAction)doSomething:(id)sender { ... if (self.rewardedAd.isReady) { [self.rewardedAd presentFromRootViewController:self delegate:self]; } else { NSLog(@"Ad wasn't ready"); } }
Receive ad event notifications
The
GADRewardedAdDelegate
provided in thepresentFromRootViewController:delegate:
method gets called when rewarded ad events occur. Each of the methods inGADRewardedAdDelegate
corresponds to an event in the lifecycle of a rewarded ad. TherewardedAd:userDidEarnReward:
method requires an implementation but all other methods for the class are marked as optional, so you only need to implement the methods you want. This example implements each method and logs a message to the console:/// Tells the delegate that the user earned a reward. - (void)rewardedAd:(GADRewardedAd *)rewardedAd userDidEarnReward:(GADAdReward *)reward { // TODO: Reward the user. NSLog(@"rewardedAd:userDidEarnReward:"); } /// Tells the delegate that the rewarded ad was presented. - (void)rewardedAdDidPresent:(GADRewardedAd *)rewardedAd { NSLog(@"rewardedAdDidPresent:"); } /// Tells the delegate that the rewarded ad failed to present. - (void)rewardedAd:(GADRewardedAd *)rewardedAd didFailToPresentWithError:(NSError *)error { NSLog(@"rewardedAd:didFailToPresentWithError"); } /// Tells the delegate that the rewarded ad was dismissed. - (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd { NSLog(@"rewardedAdDidDismiss:"); }
Using GADRewardedAdDelegate to preload the next rewarded ad
GADRewardedAd
is a one-time-use object. This means that once a rewarded ad is shown, the object can't be used to load another ad. To request another rewarded, you'll need to create a newGADRewardedAd
object.A best practice is to load another rewarded ad in the
rewardedAdDidDismiss:
method onGADRewardedAdDelegate
so that the next rewarded ad starts loading as soon as the previous one is dismissed:- (void)viewDidLoad { [super viewDidLoad]; self.rewardedAd = [self createAndLoadRewardedAd]; } - (GADRewardedAd *)createAndLoadRewardedAd { GADRewardedAd *rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:@"/6499/example/rewarded-video"]; DFPRequest *request = [DFPRequest request]; [self.rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) { if (error) { // Handle ad failed to load case. } else { // Ad successfully loaded. } }]; return rewardedAd; } - (void)rewardedAdDidDismiss:(GADRewardedAd *)rewardedAd { self.rewardedAd = [self createAndLoadRewardedAd]; }
Loading multiple rewarded ads
To load multiple rewarded ads, follow the steps outlined in the create a rewarded ad object and load an ad sections for each ad you intend to load. The code snippet below demonstrates how to load two rewarded ads for two distinct ad placements.
- (void)viewDidLoad { [super viewDidLoad]; GADRewardedAd *gameOverRewardedAd = [self createAndLoadRewardedAdForAdUnit:@"/6499/example/rewarded-video"]; GADRewardedAd *extraCoinsRewardedAd = [self createAndLoadRewardedAdForAdUnit:@"/6499/example/rewarded-video"]; } - (GADRewardedAd *)createAndLoadRewardedAdForAdUnit:(NSString *) adUnitId { GADRewardedAd *rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:adUnitId]; DFPRequest *request = [DFPRequest request]; [rewardedAd loadRequest:request completionHandler:^(GADRequestError * _Nullable error) { if (error) { // Handle ad failed to load case. } else { // Ad successfully loaded. } }]; return rewardedAd; }