|
| 1 | +// |
| 2 | +// Copyright (C) 2025 Google, Inc. |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +#import <Foundation/Foundation.h> |
| 18 | +#import <GoogleMobileAds/GoogleMobileAds.h> |
| 19 | + |
| 20 | +// Replace this ad unit ID with your own ad unit ID. |
| 21 | +static NSString *const adUnitID = @"ca-app-pub-3940256099942544/6978759866"; |
| 22 | +static NSString *const adManagerAdUnitID = @"/21775744923/example/rewarded-interstitial"; |
| 23 | + |
| 24 | +@interface RewardedInterstitialAdSnippets : UIViewController <GADFullScreenContentDelegate> |
| 25 | + |
| 26 | +@property(strong, nonatomic) GADRewardedInterstitialAd *rewardedInterstitialAd; |
| 27 | + |
| 28 | +@end |
| 29 | + |
| 30 | +@implementation RewardedInterstitialAdSnippets |
| 31 | + |
| 32 | +// [START load_ad] |
| 33 | +- (void)loadRewardedInterstitialAd { |
| 34 | + [GADRewardedInterstitialAd loadWithAdUnitID:adUnitID |
| 35 | + request:[GADRequest request] |
| 36 | + completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) { |
| 37 | + if (error) { |
| 38 | + NSLog(@"Failed to load rewarded interstitial ad with error: %@", |
| 39 | + error.localizedDescription); |
| 40 | + return; |
| 41 | + } |
| 42 | + self.rewardedInterstitialAd = ad; |
| 43 | + // [START set_delegate] |
| 44 | + self.rewardedInterstitialAd.fullScreenContentDelegate = self; |
| 45 | + // [END set_delegate] |
| 46 | + }]; |
| 47 | +} |
| 48 | +// [END load_ad] |
| 49 | + |
| 50 | +// [START load_ad_gam] |
| 51 | +- (void)loadAdManagerRewardedInterstitialAd { |
| 52 | + [GADRewardedInterstitialAd loadWithAdUnitID:adUnitID |
| 53 | + request:[GAMRequest request] |
| 54 | + completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) { |
| 55 | + if (error) { |
| 56 | + NSLog(@"Failed to load rewarded interstitial ad with error: %@", |
| 57 | + error.localizedDescription); |
| 58 | + return; |
| 59 | + } |
| 60 | + self.rewardedInterstitialAd = ad; |
| 61 | + self.rewardedInterstitialAd.fullScreenContentDelegate = self; |
| 62 | + }]; |
| 63 | +} |
| 64 | +// [END load_ad_gam] |
| 65 | + |
| 66 | +// [START validate_server_side_verification] |
| 67 | +- (void)validateServerSideVerification { |
| 68 | + // Replace this ad unit ID with your own ad unit ID. |
| 69 | + [GADRewardedInterstitialAd loadWithAdUnitID:adUnitID |
| 70 | + request:[GADRequest request] |
| 71 | + completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) { |
| 72 | + if (error) { |
| 73 | + NSLog(@"Rewarded interstitial ad failed to load with error: %@", |
| 74 | + error.localizedDescription); |
| 75 | + return; |
| 76 | + } |
| 77 | + self.rewardedInterstitialAd = ad; |
| 78 | + GADServerSideVerificationOptions *options = |
| 79 | + [[GADServerSideVerificationOptions alloc] init]; |
| 80 | + options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING"; |
| 81 | + ad.serverSideVerificationOptions = options; |
| 82 | + }]; |
| 83 | +} |
| 84 | +// [END validate_server_side_verification] |
| 85 | + |
| 86 | +// [START validate_gam_server_side_verification] |
| 87 | +- (void)validateAdManagerServerSideVerification { |
| 88 | + // Replace this ad unit ID with your own ad unit ID. |
| 89 | + [GADRewardedInterstitialAd loadWithAdUnitID:adUnitID |
| 90 | + request:[GAMRequest request] |
| 91 | + completionHandler:^(GADRewardedInterstitialAd *ad, NSError *error) { |
| 92 | + if (error) { |
| 93 | + NSLog(@"Rewarded interstitial ad failed to load with error: %@", |
| 94 | + error.localizedDescription); |
| 95 | + return; |
| 96 | + } |
| 97 | + self.rewardedInterstitialAd = ad; |
| 98 | + GADServerSideVerificationOptions *options = |
| 99 | + [[GADServerSideVerificationOptions alloc] init]; |
| 100 | + options.customRewardString = @"SAMPLE_CUSTOM_DATA_STRING"; |
| 101 | + ad.serverSideVerificationOptions = options; |
| 102 | + }]; |
| 103 | +} |
| 104 | +// [END validate_gam_server_side_verification] |
| 105 | + |
| 106 | +#pragma mark GADFullScreeContentDelegate implementation |
| 107 | + |
| 108 | +// [START ad_events] |
| 109 | +- (void)adDidRecordImpression:(id<GADFullScreenPresentingAd>)ad { |
| 110 | + NSLog(@"%s called", __PRETTY_FUNCTION__); |
| 111 | +} |
| 112 | + |
| 113 | +- (void)adDidRecordClick:(id<GADFullScreenPresentingAd>)ad { |
| 114 | + NSLog(@"%s called", __PRETTY_FUNCTION__); |
| 115 | +} |
| 116 | + |
| 117 | +- (void)adWillPresentFullScreenContent:(id<GADFullScreenPresentingAd>)ad { |
| 118 | + NSLog(@"%s called", __PRETTY_FUNCTION__); |
| 119 | +} |
| 120 | + |
| 121 | +- (void)adWillDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad { |
| 122 | + NSLog(@"%s called", __PRETTY_FUNCTION__); |
| 123 | +} |
| 124 | + |
| 125 | +- (void)adDidDismissFullScreenContent:(id<GADFullScreenPresentingAd>)ad { |
| 126 | + NSLog(@"%s called", __PRETTY_FUNCTION__); |
| 127 | + // Clear the rewarded interstitial ad. |
| 128 | + self.rewardedInterstitialAd = nil; |
| 129 | +} |
| 130 | + |
| 131 | +- (void)ad:(id)ad didFailToPresentFullScreenContentWithError:(NSError *)error { |
| 132 | + NSLog(@"%s called with error: %@", __PRETTY_FUNCTION__, error.localizedDescription); |
| 133 | +} |
| 134 | +// [END ad_events] |
| 135 | + |
| 136 | +// [START show_ad] |
| 137 | +- (void)showRewardedInterstitialAd { |
| 138 | + [self.rewardedInterstitialAd presentFromRootViewController:self |
| 139 | + userDidEarnRewardHandler:^{ |
| 140 | + GADAdReward *reward = self.rewardedInterstitialAd.adReward; |
| 141 | + |
| 142 | + NSString *rewardMessage = [NSString |
| 143 | + stringWithFormat:@"Reward received with " |
| 144 | + @"currency %@ , amount %ld", |
| 145 | + reward.type, [reward.amount longValue]]; |
| 146 | + NSLog(@"%@", rewardMessage); |
| 147 | + // TODO: Reward the user. |
| 148 | + }]; |
| 149 | +} |
| 150 | +// [END show_ad] |
| 151 | + |
| 152 | +@end |
0 commit comments