Rewarded interstitial is a type of incentivized ad format that allows you offer rewards for ads that appear automatically during natural app transitions. Unlike rewarded ads, users aren't required to opt-in to view a rewarded interstitial.
Prerequisites
- Google Mobile Ads SDK 7.60.0 or higher.
- Complete the Get started guide.
Implementation
The primary steps to integrate rewarded interstitial ads are as follows:
- Load an ad
- [Optional] Validate SSV callbacks
- Register for callbacks
- Display the ad and handle the reward event
Load an ad
Loading an ad is accomplished using the static
loadWithAdUnitID:request:completionHandler:
method on the
GADRewardedInterstitialAd
class. The load method requires your ad unit ID, a
GAMRequest
object, and a completion handler
which gets called when ad loading succeeds or fails. The loaded
GADRewardedInterstitialAd
object is provided as a parameter in the completion
handler. The below example shows how to load a GADRewardedInterstitialAd
in
your ViewController
class.
Swift
import GoogleMobileAds import UIKit class ViewController: UIViewController { private var rewardedInterstitialAd: GADRewardedInterstitialAd? override func viewDidLoad() { super.viewDidLoad() GADRewardedInterstitialAd.load(withAdUnitID:"/21775744923/example/rewarded_interstitial", request: GAMRequest()) { ad, error in if let error = error { return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") } self.rewardedInterstitialAd = ad } } }
Objective-C
#import "ViewController.h" @interface ViewController () @property(nonatomic, strong) GADRewardedInterstitialAd* rewardedInterstitialAd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; [GADRewardedInterstitialAd loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial" request:[GAMRequest request] completionHandler:^( GADRewardedInterstitialAd* _Nullable rewardedInterstitialAd, NSError* _Nullable error) { if (!error) { self.rewardedInterstitialAd = rewardedInterstitialAd; } } ]; }
[Optional] Validate server-side verification (SSV) callbacks
Apps that require extra data in server-side
verification callbacks should use the
custom data feature of rewarded ads. Any string value set on a rewarded ad
object is passed to the custom_data
query parameter of the SSV callback. If no
custom data value is set, the custom_data
query parameter value won't be
present in the SSV callback.
The following code sample demonstrates how to set custom data on a rewarded interstitial ad object before requesting an ad.
Swift
GADRewardedInterstitialAd.load(withAdUnitID:"/21775744923/example/rewarded_interstitial", request: request, completionHandler: { [self] ad, error in if let error != error { rewardedInterstitialAd = ad let options = GADServerSideVerificationOptions() options.customRewardString = "SAMPLE_CUSTOM_DATA_STRING" rewardedInterstitialAd.serverSideVerificationOptions = options }
Objective-C
GAMRequest *request = [GAMRequest request]; [GADRewardedInterstitialAd loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial" request:request completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) { if (error) { // Handle Error return; } self.rewardedInterstitialAd = ad; GADServerSideVerificationOptions *options = [[GADServerSideVerificationOptions alloc] init]; options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING"; ad.serverSideVerificationOptions = options; }];
Register for callbacks
In order to receive notifications for presentation events, you must implement
the GADFullScreenContentDelegate
protocol and assign it to the
fullScreenContentDelegate
property of the returned ad. The
GADFullScreenContentDelegate
protocol handles callbacks for when the ad
presents successfully or unsuccessfully, and when it is dismissed. The following
code shows how to implement the protocol and assign it to the ad:
Swift
class ViewController: UIViewController { private var rewardedInterstitialAd: GADRewardedInterstitialAd? override func viewDidLoad() { super.viewDidLoad() GADRewardedInterstitialAd.load(withAdUnitID:"/21775744923/example/rewarded_interstitial", request: GAMRequest()) { ad, error in if let error = error { return print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") } self.rewardedInterstitialAd = ad self.rewardedInterstitialAd?.fullScreenContentDelegate = self } } } extension ViewController: GADFullScreenContentDelegate { /// Tells the delegate that the ad failed to present full screen content. func ad(_ ad: GADFullScreenPresentingAd, didFailToPresentFullScreenContentWithError error: Error) { print("Ad did fail to present full screen content.") } /// Tells the delegate that the ad will present full screen content. func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) { print("Ad will present full screen content.") } /// Tells the delegate that the ad dismissed full screen content. func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { print("Ad did dismiss full screen content.") } }
Objective-C
@interface ViewController ()<GADFullScreenContentDelegate> @property(nonatomic, strong) GADRewardedInterstitialAd *rewardedInterstitialAd; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [GADRewardedInterstitialAd loadWithAdUnitID:@"/21775744923/example/rewarded_interstitial" request:[GAMRequest request] completionHandler:^( GADRewardedInterstitialAd *_Nullable rewardedInterstitialAd, NSError *_Nullable error) { if (!error) { self.rewardedInterstitialAd = rewardedInterstitialAd; self.rewardedInterstitialAd.fullScreenContentDelegate = self; } }]; } /// Tells the delegate that the ad failed to present full screen content. - (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error { NSLog(@"Ad did fail to present full screen content."); } /// Tells the delegate that the ad will present full screen content. - (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad { NSLog(@"Ad will present full screen content."); } /// Tells the delegate that the ad dismissed full screen content. - (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad { NSLog(@"Ad did dismiss full screen content."); }
Display the ad and handle the reward event
When presenting your ad, you must provide a GADUserDidEarnRewardHandler
object
to handle the reward for the user.
The following code presents the best method for displaying a rewarded interstitial ad.
Swift
func show() { guard let rewardedInterstitialAd = rewardedInterstitialAd else { return print("Ad wasn't ready.") } rewardedInterstitialAd.present(fromRootViewController: self) { let reward = rewardedInterstitialAd.adReward // TODO: Reward the user! } }
Objective-C
- (void)show { [_rewardedInterstitialAd presentFromRootViewController:self userDidEarnRewardHandler:^{ GADAdReward *reward = self.rewardedInterstitialAd.adReward; // TODO: Reward the user! }]; }
Next steps
Learn more about user privacy.