Rewarded Ads
Prerequisites
- Follow the initial integration guide for iOS
- Ensure that you have a Rewarded Ad Placement ID to use.
Display a rewarded ad in your app
Displaying a rewarded ad is a three-step process. First you should create and load your MFRewardedAd unit, then when you’re ready, call the showAd() method to display the ad. Finally you should reward the user.
- Create an rewarded ad unit
- Display the ad
- Reward the user
Create an rewarded ad unit
The MFRewardedAd
class provided by the MobileFuse SDK will handle the loading and display of the full-screen ads.
Tip
To provide a good user experience, you should create and pre-load your rewarded ad units before you intend to show the rewarded ad. Once an rewarded ad unit is created and pre-loaded, you can display it instantly with the showAd() method. Having the ad preloaded will ensure that you’re ready to display the ad as soon as the rewarded opportunity occurs. However, please note that ads expire after 5 minutes (at which time you’ll need to request a new one), so plan accordingly.
First, create a new MFRewardedAd
unit and keep a reference to it:
@property (nonatomic, strong, readwrite) MFRewardedAd *rewardedAd;
self.rewardedAd = [[MFRewardedAd alloc] initWithPlacementId:@"<Rewarded placement ID>"];
Note
Ensure that the SDK has fully initialized before creating your ad units. You can use the onInitSuccess callback as a good place to set up your ad units.
Next, add a delegate receiver - this will allow you to receive events from the ad unit.
[self.rewardedAd registerAdCallbackReceiver:self];
// ...
// IMFAdCallbackReceiver delegate implementation
- (void)onUserEarnedReward:(MFAd *)ad {
// The user has watched this rewarded ad and earned the reward for doing so - you should
// reward the user accordingly from this callback.
NSLog(@"IMFAdCallbackReceiver::onUserEarnedReward with ad instance: %@ (Placement ID: %@)", [ad description], ad.placementId);
}
- (void)onAdLoaded:(MFAd *)ad {
// Ad has loaded - you are able to show the ad after this callback is triggered
NSLog(@"IMFAdCallbackReceiver::onAdLoaded with ad instance: %@ (Placement ID: %@)", [ad description], ad.placementId);
}
- (void)onAdRendered:(MFAd *)ad {
// Triggered when the ad begins to show to the user
NSLog(@"IMFAdCallbackReceiver::onAdRendered with ad instance: %@ (Placement ID: %@)", [ad description], ad.placementId);
}
- (void)onAdClicked:(MFAd *)ad {
// Triggered when the ad is clicked by the user
NSLog(@"IMFAdCallbackReceiver::onAdClicked with ad instance: %@ (Placement ID: %@)", [ad description], ad.placementId);
}
- (void)onAdNotFilled:(MFAd *)ad {
// No ad is currently available to display to this user
NSLog(@"IMFAdCallbackReceiver::onAdClicked with ad instance: %@ (Placement ID: %@)", [ad description], ad.placementId);
}
- (void)onAdClosed:(MFAd *)ad {
// The ad has been displayed and closed
NSLog(@"IMFAdCallbackReceiver::onAdClosed with ad instance: %@ (Placement ID: %@)", [ad description], ad.placementId);
}
- (void)onAdExpired:(MFAd *)ad {
// Triggered when a loaded ad has expired - you should manually try to load a new ad here
NSLog(@"IMFAdCallbackReceiver::onAdExpired with ad instance: %@ (Placement ID: %@)", [ad description], ad.placementId);
}
@interface ViewController : UIViewController<..., IMFAdCallbackReceiver>
Add the ad instance as a subview
[self.view addSubview:self.rewardedAd];
Finally, request that the ad is preloaded using the loadAd method:
[self.rewardedAd loadAd];
It’s also recommended you ensure that UI manipulation stays on the main thread. Here is a complete example:
// UI on main thread
dispatch_async(dispatch_get_main_queue(), ^ {
self.rewardedAd = [[MFRewardedAd alloc] initWithPlacementId:self.rewardedPlacementId];
[self.rewardedAd registerAdCallbackReceiver:self];
[self.view addSubview:self.rewardedAd];
[self.rewardedAd loadAd];
});
Display the ad
Once the rewarded ad has triggered the onAdLoaded callback, you can then call showAd to instantly display the ad:
[self.rewardedAd showAd];
You can also use isLoaded
to check whether the ad is ready to be displayed.
if ([self.rewardedAd isLoaded]) {
// The ad is ready to be displayed!
}
Note
Once you call
showAd
on an instance, the loaded ad will be displayed. Since a loaded ad can only be displayed once, you must callloadAd
again once the ad is closed to get a new ad ready to display.
Reward the user
The onUserEarnedReward
callback will fire when the ad has been viewed by the user for long enough to earn the reward. You should use this callback to grant the reward to your user:
- (void)onUserEarnedReward:(MFAd *)ad {
// For example:
[Player addCoins:20];
}
Updated about 2 years ago