Rewarded ads are only available from third-party networks that you've selected for mediation.
Prerequisites
- Xcode 5.1 or higher
- Deployment target of 6.0 or higher
- Google Mobile Ads SDK 7.7.0 or higher
Download the example
Download the example application to see mediated rewarded ads in action. You need to include any mediation adapters, ad network specific configurations, and an ad unit ID, which is configured for rewarded video mediation.
Add mediation to your project
Follow the Adding the SDK to your Xcode project section of Get Started to import the Google Mobile Ads SDK into your project.
Add network adapters and SDKs
You're now ready to download and add to your project the adapters and SDKs of all the ad networks from which you'd like to serve rewarded video ads.
To add the downloaded network adapters/SDKs in Xcode, right-click on your project and click Add Files to Project.
You may consider integrating adapters and SDKs from networks that you're not currently using but might use in the future. If you do this, you'll be able to start serving ads from these networks simply by making a configuration change in your AdMob account without requiring a code change to your app. Weigh this against the increase in app binary size that each additional SDK adds.
Include network configurations
You need to include any frameworks, compiler flags, or linker flags that your chosen networks require. Identify any extra configuration required in the documentation for the ad networks you're mediating.
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:
[[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request]
withAdUnitID:@"ca-app-pub-1234567890123456/1234567890"];
To allow videos to be preloaded, it is highly recommended to call
loadRequest()
as early as possible (for example, in your app
delegate's application:didFinishLaunchingWithOptions()
method).
Set up event notification
To set up event notification, insert the line shown in bold before your loadRequest() call:
[GADRewardBasedVideoAd sharedInstance].delegate = self; [[GADRewardBasedVideoAd sharedInstance] loadRequest:[GADRequest request] withAdUnitID:@"ca-app-pub-1234567890123456/1234567890"];
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.
Here is an example that logs each event available in
GADRewardBasedVideoAdDelegate:
:
- (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)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.");
}
Additional parameters
Ad networks may support extra targeting parameters or inputs
that are not covered by the additional request parameters
provided by the Google Mobile Ads SDK.
These ad networks may provide a class
that implements the GADAdNetworkExtras
protocol.
You can pass each ad network
an instance of the GADAdNetworkExtras
subclass
when you build the ad request.
Suppose an ad network created a class that supported a muteAudio
parameter.
Let's call that class GADMMyAdNetworkExtras
for the sake of this example.
Here is how you could create this object and add it to the request:
GADRequest *request = [GADRequest request];
GADMMyAdNetworkExtras *myAdNetworkExtras = [[GADMMyAdNetworkExtras alloc] init];
myAdNetworkExtras.muteAudio = YES;
[request registerAdNetworkExtras:myAdNetworkExtras];
The GADMMyAdNetworkExtras
class is used as an example
in the code snippet above. Substitute the adapter class
corresponding to the network for which your bundle is intended.
If an adapter supports extra parameters, refer to the ad network's documentation to learn how to pass extra parameters to that network.
Display rewarded video
It is a best practice to ensure 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:
if ([[GADRewardBasedVideoAd sharedInstance] isReady]) {
[[GADRewardBasedVideoAd sharedInstance] presentFromRootViewController:self];
}
Integration with Unity
For instructions on integrating rewarded video mediation into a Unity project, see Rewarded Video Mediation.