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
Create rewarded ad
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 a GADRewardedAd
in the viewDidLoad:
method of a
UIViewController
:
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { /// The rewarded video ad. var rewardedAd: GADRewardedAd? override func viewDidLoad() { super.viewDidLoad() rewardedAd = GADRewardedAd(adUnitID: "/6499/example/rewarded") } }
Objective-C
@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"]; }
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
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 ad
To load a rewarded ad, call
loadRequest:completionHandler:
on a GADRewardedAd
object:
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController, GADRewardedAdDelegate { /// The rewarded video ad. var rewardedAd: GADRewardedAd? override func viewDidLoad() { super.viewDidLoad() rewardedAd = GADRewardedAd(adUnitID: "/6499/example/rewarded") rewardedAd?.load(GADRequest()) { error in if let error = error { // Handle ad failed to load case. } else { // Ad successfully loaded. } } }
Objective-C
@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"]; 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 nil
GADRequestError
parameter. If ad load failed, the non-nil error object
provides failure information.
Display 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 on GADRewardedAd
to
verify that it's finished loading, then call
presentFromRootViewController:delegate:
. Here's an example of how to do this
in one of the action methods in a UIViewController
:
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController, GADRewardedAdDelegate { /// The rewarded video ad. var rewardedAd: GADRewardedAd? ... @IBAction func doSomething(sender: UIButton) { if rewardedAd?.isReady == true { rewardedAd?.present(fromRootViewController: self, delegate:self) } } }
Objective-C
@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 the
presentFromRootViewController:delegate:
method gets called when rewarded ad
events occur. Each of the methods in GADRewardedAdDelegate
corresponds to an
event in the lifecycle of a rewarded ad. The rewardedAd: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:
Swift
/// Tells the delegate that the user earned a reward. func rewardedAd(_ rewardedAd: GADRewardedAd, userDidEarn reward: GADAdReward) { print("Reward received with currency: \(reward.type), amount \(reward.amount).") } /// Tells the delegate that the rewarded ad was presented. func rewardedAdDidPresent(_ rewardedAd: GADRewardedAd) { print("Rewarded ad presented.") } /// Tells the delegate that the rewarded ad was dismissed. func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) { print("Rewarded ad dismissed.") } /// Tells the delegate that the rewarded ad failed to present. func rewardedAd(_ rewardedAd: GADRewardedAd, didFailToPresentWithError error: Error) { print("Rewarded ad failed to present.") }
Objective-C
/// 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 new GADRewardedAd
object.
A best practice is to load another rewarded ad in the rewardedAdDidDismiss:
method on GADRewardedAdDelegate
so that the next rewarded ad starts loading as
soon as the previous one is dismissed:
Swift
override func viewDidLoad() { super.viewDidLoad() rewardedAd = createAndLoadRewardedAd() } func createAndLoadRewardedAd() { rewardedAd = GADRewardedAd(adUnitID: "/6499/example/rewarded") rewardedAd?.load(GADRequest()) { error in if let error = error { print("Loading failed: \(error)") } else { print("Loading Succeeded") } } return rewardedAd } func rewardedAdDidDismiss(_ rewardedAd: GADRewardedAd) { rewardedAd = createAndLoadRewardedAd() }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; self.rewardedAd = [self createAndLoadRewardedAd]; } - (GADRewardedAd *)createAndLoadRewardedAd { GADRewardedAd *rewardedAd = [[GADRewardedAd alloc] initWithAdUnitID:@"/6499/example/rewarded"]; 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; } - (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.
Swift
override func viewDidLoad() { super.viewDidLoad() rewardedAd1 = createAndLoadRewardedAd("first-ad-unit-id") rewardedAd2 = createAndLoadRewardedAd("second-ad-unit-id") } func createAndLoadRewardedAd(adUnitId) { rewardedAd = GADRewardedAd(adUnitID: adUnitId) rewardedAd?.load(GADRequest()) { error in if let error = error { print("Loading failed: \(error)") } else { print("Loading Succeeded") } } return rewardedAd }
Objective-C
- (void)viewDidLoad { [super viewDidLoad]; GADRewardedAd *gameOverRewardedAd = [self createAndLoadRewardedAdForAdUnit:@"/6499/example/rewarded"]; GADRewardedAd *extraCoinsRewardedAd = [self createAndLoadRewardedAdForAdUnit:@"/6499/example/rewarded"]; } - (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; }