Add Rewarded Interstitial Ads to an iOS App

Rewarded interstitial ads are a full screen experience which starts when users reach a natural break in a game, and which offers rewards in exchange for a user viewing the full ad. Users can opt-out at any time. The ad experience is 15-30 seconds and contains an end card with a call to action. Upon completion of the full video, you will receive a callback to grant the suggested reward to the user.

Below are details on how to implement rewarded video ads from Audience Network on iOS.

Set up the SDK

The Audience Network Rewarded Video format is now included in the public SDK. Rewarded video will be available for all gaming apps soon. If you don’t see Rewarded Video in Monetization Manager and you're on the latest SDK, apply now.

確保您在開始操作前,已經先行參閱 Audience Network 新手指南iOS 新手指南

Implementation

Now, in your View Controller header file (or Swift file, if you are a Swift user), import FBAudienceNetwork, declare conformance to the FBRewardedInterstitialAdDelegate protocol, and add an instance variable for the rewarded video ad unit

import UIKit
import FBAudienceNetwork

class ViewController: UIViewController, FBRewardedInterstitialAdDelegate  {
  private var rewardedInterstitialAd: FBRewardedInterstitialAd?
}
#import <UIKit/UIKit.h>
@import FBAudienceNetwork;

@interface ViewController : UIViewController <FBRewardedInterstitialAdDelegate>

@property (nonatomic, strong) FBRewardedInterstitialAd *rewardedInterstitialAd;

@end

Add a function in your View Controller that initializes the rewarded interstitial ad object and caches the video creative ahead of the time you want to show it

override func viewDidLoad() {
  super.viewDidLoad()
  
  // Instantiate an rewarded interstitial object.
  // NOTE: the placement ID will eventually identify this as your app. You can ignore it while you are testing
  // and replace it later when you have signed up.
  // While you are using this temporary code you will only get test ads and if you release
  // your code like this to the App Store your users will not receive ads (you will get a 'No Fill' error).
  let rewardedInterstitialAd = FBRewardedInterstitialAd(placementID: "YOUR_PLACEMENT_ID")
  rewardedInterstitialAd.delegate = self
  
  // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
  rewardedInterstitialAd.load()
  
  self.rewardedInterstitialAd = rewardedInterstitialAd
}
- (void) viewDidLoad 
{
  [super viewDidLoad];
  // Instantiate a rewarded interstitial ad object. 
  // NOTE: the placement ID will eventually identify this as your app. You can ignore it while 
  // you are testing and replace it later when you have signed up.
  // While you are using this temporary code you will only get test ads and if you release
  // your code like this to the App Store your users will not receive ads (you will get a 'No Fill' error).
  self.rewardedInterstitialAd = [[FBRewardedInterstitialAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
  self.rewardedInterstitialAd.delegate = self;

  // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
  [self.rewardedInterstitialAd loadAd];
}

The ID that displays at YOUR_PLACEMENT_ID is a temporary ID for test purposes only.

If you use this temporary ID in your live code, your users will not receive ads (they will get a No Fill error). You must return here after testing and replace this temporary ID with a live Placement ID.

To find out how the generate a live Placement ID, refer to Audience Network Setup

Now that you have added the code to load the ad, you can add functions which will handle various events.

For example:

  • rewardedInterstitialAdDidLoad ensures your app is aware when the ad is cached and ready to be displayed
  • rewardedInterstitialAdVideoComplete lets you know when to deliver your reward to the user after a completed video view
func rewardedInterstitialAdDidLoad(_ rewardedInterstitialAd: FBRewardedInterstitialAd) {
  print("Video ad is loaded and ready to be displayed")
}

func rewardedInterstitialAd(_ rewardedInterstitialAd: FBRewardedInterstitialAd, didFailWithError error: Error) {
  print("Rewarded Interstitial ad failed to load")
}

func rewardedInterstitialAdDidClick(_ rewardedInterstitialAd: FBRewardedInterstitialAd) {
  print("Video ad clicked")
}

func rewardedInterstitialAdDidClose(_ rewardedInterstitialAd: FBRewardedInterstitialAd) {
  print("Rewarded Interstitial ad closed - this can be triggered by closing the application, or closing the video end card")
}

func rewardedInterstitialAdVideoComplete(_ rewardedInterstitialAd: FBRewardedInterstitialAd) {
  print("Rewarded Interstitial ad video completed - this is called after a full video view, before the ad end card is shown. You can use this event to initialize your reward")
}
- (void)rewardedInterstitialAdDidLoad:(FBRewardedInterstitialAd *)rewardedInterstitialAd
{
  NSLog(@"Video ad is loaded and ready to be displayed");
}
    
- (void)rewardedInterstitialAd:(FBRewardedInterstitialAd *)rewardedInterstitialAd didFailWithError:(NSError *)error
{
  NSLog(@"Rewarded video ad failed to load");
}

- (void)rewardedInterstitialAdDidClick:(FBRewardedInterstitialAd *)rewardedInterstitialAd
{
  NSLog(@"Video ad clicked");
}
    
- (void)rewardedInterstitialAdDidClose:(FBRewardedInterstitialAd *)rewardedInterstitialAd
{
  NSLog(@"Rewarded Interstitial ad closed - this can be triggered by closing the application, or closing the video end card");
}

- (void)rewardedInterstitialAdVideoComplete:(FBRewardedInterstitialAd *)rewardedInterstitialAd;
{
  NSLog(@"Rewarded Interstitial ad video completed - this is called after a full video view, before the ad end card is shown. You can use this event to initialize your reward");
}

Finally, when you are ready to show the rewarded video ad you can call the following code within your own reward function.

private func showRewardedInterstitialAd() {
  guard let rewardedInterstitialAd = rewardedInterstitialAd, rewardedInterstitialAd.isAdValid else {
    return
  }
  rewardedInterstitialAd.show(fromRootViewController: self)
}
- (void)showRewardedInterstitialAd
{
  if (self.rewardedInterstitialAd && self.rewardedInterstitialAd.isAdValid) {
    [self.rewardedInterstitialAd showAdFromRootViewController:self];
  }
}

The method to show a rewarded video ad includes an animated boolean flag which allows you to animate the presentation. By default it is set to YES / true, but you can override it.

When running on the simulator, test ads will be shown by default. To enable test ads on a device, add the following line of code before loading an ad: AdSettings.addTestDevice(HASHED ID). Use the hashed ID that is printed to the log cat when you first make a request to load an ad on a device.

Optionally, you can add the following additional functions to handle the cases where the rewarded video ad will close or when the rewarded video impression is being captured:

func rewardedInterstitialAdWillClose(_ rewardedInterstitialAd: FBRewardedInterstitialAd) {
  print("The user clicked on the close button, the ad is just about to close")
}

func rewardedInterstitialAdWillLogImpression(_ rewardedInterstitialAd: FBRewardedInterstitialAd) {
  print("Rewarded Interstitial impression is being captured")
}
- (void)rewardedInterstitialAdWillClose:(FBRewardedInterstitialAd *)rewardedInterstitialAd
{
  NSLog(@"The user clicked on the close button, the ad is just about to close");
}

- (void)rewardedInterstitialAdWillLogImpression:(FBRewardedInterstitialAd *)rewardedInterstitialAd
{
  NSLog(@"Rewarded Video impression is being captured");
}

後續步驟

  • 探索程式碼範例,瞭解如何使用原生廣告。SDK 在 NativeAdSample 資料夾中提供了 FBAudienceNetwork/samples。以 Xcode 開啟專案,然後在裝置或模擬器上執行該專案。