(EN) iOS Rewarded Video - adxcorp/ADXLibrary_Integration GitHub Wiki

AdMob Rewarded Video Ad Integration

Loading Rewarded Video Ads take some time. It is recommended that you pre-load it before using it.

  1. Before loading ads, call the startWithCompletionHandler: method on the GADMobileAds.sharedInstance, which initializes the SDK.
  2. Add GADFullScreenContentDelegate delegate
  3. Create GADRequest. If you require GDPR consent, add npa data as extra (Set npa to '1' only when state is ADXConsentStateDenied)
  4. Call GADRewardedAd 's loadWithAdUnitID: request: completionHandler: to load ad
  5. Call presentFromRootViewController: userDidEarnRewardHandler: to show rewarded video ad
  6. When rewarded video ad has finished playing, GADUserDidEarnRewardHandler object to handle the reward for the user.

AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [[GADMobileAds sharedInstance] startWithCompletionHandler:nil];
    
    return YES;
}

MyViewController.m

@import GoogleMobileAds;

@interface ADXAdMobRewardViewController () <GADFullScreenContentDelegate>

@end

@implementation ADXAdMobRewardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createAndLoadRewardedAd];
}

- (GADRewardedAd *)createAndLoadRewardedAd {                   
  GADRequest *request = [GADRequest request];
  //*** GDPR
  if ([ADXGDPR.sharedInstance getConsentState] == ADXConsentStateDenied) {
        GADExtras *extras = [[GADExtras alloc] init];
        extras.additionalParameters = @{@"npa": @"1"};
        [request registerAdNetworkExtras:extras];
  }
  [GADRewardedAd loadWithAdUnitID:@"<YOUR AD UNIT ID HERE>" request:request completionHandler:^(GADRewardedAd *rewardedAd, NSError *error) {
        if (error) {
            // Handle ad failed to load case.
        } else {
            // Ad successfully loaded.
            self.rewardedAd = rewardedAd;
            self.rewardedAd.fullScreenContentDelegate = self;
        }
    }]; 
}

- (IBAction)selectShowAd:(id)sender {
    if (self.rewardedAd) {
        [self.rewardedAd presentFromRootViewController:self userDidEarnRewardHandler:^{
            GADAdReward *reward = self.rewardedAd.adReward;
            // TODO: Reward the uesr!
        }];
    } else {
        NSLog(@"Ad wasn't ready");
        [self createAndLoadRewardedAd];
    }
}

#pragma mark - GADFullScreenContentDelegate

/// Tells the delegate that an impression has been recorded for the ad.
- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad {
    NSLog(@"adDidRecordImpression");
}

/// Tells the delegate that the ad failed to present full screen content.
- (void)ad:(id<GADFullScreenPresentingAd>)ad didFailToPresentFullScreenContentWithError:(NSError *)error {
    NSLog(@"Rewarded ad failed to present.");
}

/// Tells the delegate that the ad presented full screen content.
- (void)adDidPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
    NSLog(@"Rewarded ad presented");
}

/// Tells the delegate that the ad dismissed full screen content.
- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad {
    NSLog(@"adDidDismissFullScreenContent");
    [self createAndLoadRewardedAd];
}

MoPub Rewarded Video Ad Integration

Loading Rewarded Video Ads take some time. It is recommended that you pre-load it before using it.

  1. Add delegate to MPRewardedAds
  2. Call MPRewardedAds's loadRewardedAdWithAdUnitID: to load ad
  3. rewardedAdDidLoadForAdUnitID: gets called when ad has been loaded
  4. Call MPRewardedAds's hasAdAvailableForAdUnitID: to make sure there is an ad to display then call presentRewardedAdForAdUnitID: to show rewarded video ad
  5. When rewarded video ad has finished playing, rewardedAdShouldRewardForAdUnitID:reward: gets called. Once the callback gets called, then you may proceed to reward the user.

MyViewController.m

#import <MoPub.h>

@interface ADXRewardViewController () <MPRewardedAdsDelegate>

@end

@implementation ADXRewardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [MPRewardedAds setDelegate:self forAdUnitId:<YOUR AD UNIT ID HERE>];
    [MPRewardedAds loadRewardedAdWithAdUnitID:<YOUR AD UNIT ID HERE> withMediationSettings:nil];
}

- (IBAction)selectShowAd:(id)sender {
    
    if ([MPRewardedAds hasAdAvailableForAdUnitID:<YOUR AD UNIT ID HERE>]) {
        [MPRewardedAds presentRewardedAdForAdUnitID:<YOUR AD UNIT ID HERE> fromViewController:self withReward:nil customData:nil];
    } else {
        [MPRewardedAds loadRewardedAdWithAdUnitID:<YOUR AD UNIT ID HERE> withMediationSettings:nil];
    }
}

#pragma mark - MPRewardedAdsDelegate

- (void)rewardedAdDidLoadForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdDidLoadForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdDidFailToLoadForAdUnitID:(NSString *)adUnitID error:(NSError *)error {
    NSLog(@"rewardedAdDidFailToLoadForAdUnitID : %@\nerror : %@", adUnitID, error.localizedDescription);
}

- (void)rewardedAdDidExpireForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdDidExpireForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdDidFailToShowForAdUnitID:(NSString *)adUnitID error:(NSError *)error {
    NSLog(@"rewardedAdDidFailToShowForAdUnitID : %@\n error : %@", adUnitID, error.localizedDescription);
}

- (void)rewardedAdWillPresentForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdWillPresentForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdDidPresentForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdDidPresentForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdWillDismissForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdWillDismissForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdDidDismissForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdDidDismissForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdDidReceiveTapEventForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdDidReceiveTapEventForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdWillLeaveApplicationForAdUnitID:(NSString *)adUnitID {
    NSLog(@"rewardedAdWillLeaveApplicationForAdUnitID : %@", adUnitID);
}

- (void)rewardedAdShouldRewardForAdUnitID:(NSString *)adUnitID reward:(MPReward *)reward {
    NSLog(@"rewardedAdShouldRewardForAdUnitID : %@\n reward : %@", adUnitID, reward.currencyType);
}
⚠️ **GitHub.com Fallback** ⚠️