Skip to content

Commit 2b0b0f8

Browse files
JillSongcopybara-github
authored andcommitted
Added region tags for rewarded interstitial snippets
PiperOrigin-RevId: 794125133
1 parent fd62826 commit 2b0b0f8

File tree

4 files changed

+303
-1
lines changed

4 files changed

+303
-1
lines changed

Objective-C/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
AED1111020320CA900EA4BEE /* NativeAdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = AED1110F20320CA800EA4BEE /* NativeAdView.xib */; };
3636
AEE8DA9F201BD7A6008792E6 /* SimpleNativeAdView.m in Sources */ = {isa = PBXBuildFile; fileRef = AEE8DA9D201BD7A6008792E6 /* SimpleNativeAdView.m */; };
3737
AEE8DAA5201BDCA9008792E6 /* CustomControlsView.m in Sources */ = {isa = PBXBuildFile; fileRef = AEE8DAA4201BDCA9008792E6 /* CustomControlsView.m */; };
38+
EF25D0222E4B916800688F43 /* BannerSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0212E4B916800688F43 /* BannerSnippets.m */; };
39+
EF25D0242E4B917500688F43 /* ResponseInfoSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0232E4B917500688F43 /* ResponseInfoSnippets.m */; };
40+
EF25D0262E4B95F400688F43 /* RewardedInterstitialAdSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0252E4B917E00688F43 /* RewardedInterstitialAdSnippets.m */; };
3841
EF98F6062BD948C900FDCA41 /* CollapsibleBannerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = EF98F6052BD948C900FDCA41 /* CollapsibleBannerViewController.m */; };
3942
/* End PBXBuildFile section */
4043

@@ -85,6 +88,9 @@
8588
AEE8DA9D201BD7A6008792E6 /* SimpleNativeAdView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SimpleNativeAdView.m; sourceTree = "<group>"; };
8689
AEE8DAA3201BDCA9008792E6 /* CustomControlsView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CustomControlsView.h; sourceTree = "<group>"; };
8790
AEE8DAA4201BDCA9008792E6 /* CustomControlsView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CustomControlsView.m; sourceTree = "<group>"; };
91+
EF25D0212E4B916800688F43 /* BannerSnippets.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BannerSnippets.m; sourceTree = "<group>"; };
92+
EF25D0232E4B917500688F43 /* ResponseInfoSnippets.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ResponseInfoSnippets.m; sourceTree = "<group>"; };
93+
EF25D0252E4B917E00688F43 /* RewardedInterstitialAdSnippets.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RewardedInterstitialAdSnippets.m; sourceTree = "<group>"; };
8894
EF98F6042BD948A400FDCA41 /* CollapsibleBannerViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CollapsibleBannerViewController.h; sourceTree = "<group>"; };
8995
EF98F6052BD948C900FDCA41 /* CollapsibleBannerViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = CollapsibleBannerViewController.m; sourceTree = "<group>"; };
9096
/* End PBXFileReference section */
@@ -184,6 +190,9 @@
184190
A8B6503B2DD63FAB00AE1D3F /* Snippets */ = {
185191
isa = PBXGroup;
186192
children = (
193+
EF25D0252E4B917E00688F43 /* RewardedInterstitialAdSnippets.m */,
194+
EF25D0232E4B917500688F43 /* ResponseInfoSnippets.m */,
195+
EF25D0212E4B916800688F43 /* BannerSnippets.m */,
187196
A86568AC2E254DD300D360C3 /* ATTSnippets.m */,
188197
A8265F722E4F8AE000A9C150 /* BannerSnippets.m */,
189198
A8265F762E4F8AEF00A9C150 /* NativeAdOptionsSnippets.m */,
@@ -284,6 +293,8 @@
284293
isa = PBXSourcesBuildPhase;
285294
buildActionMask = 2147483647;
286295
files = (
296+
EF25D0262E4B95F400688F43 /* RewardedInterstitialAdSnippets.m in Sources */,
297+
EF25D0242E4B917500688F43 /* ResponseInfoSnippets.m in Sources */,
287298
4A96E7241B856D5000031470 /* AppDelegate.m in Sources */,
288299
4A96E7531B8578D600031470 /* AdMobBannerSizesViewController.m in Sources */,
289300
AEC9AC0D2140985100E1DC91 /* AdMobNativeCustomMuteThisAdViewController.m in Sources */,
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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

Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
AED11120203213FC00EA4BEE /* UnifiedNativeAdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = AED11118203213F500EA4BEE /* UnifiedNativeAdView.xib */; };
3636
AED111262032151200EA4BEE /* SimpleNativeAdView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AED111252032151200EA4BEE /* SimpleNativeAdView.swift */; };
3737
AED111282032174100EA4BEE /* CustomControlsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AED111272032174100EA4BEE /* CustomControlsView.swift */; };
38+
EF25D0282E4B97F000688F43 /* BannerSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0272E4B97F000688F43 /* BannerSnippets.swift */; };
39+
EF25D02A2E4B97FA00688F43 /* ResponseInfoSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0292E4B97FA00688F43 /* ResponseInfoSnippets.swift */; };
40+
EF25D02C2E4B980C00688F43 /* RewardedInterstitialAdSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF25D02B2E4B980100688F43 /* RewardedInterstitialAdSnippets.swift */; };
3841
EFC712462AD853D80014A2BF /* CollapsibleBannerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFC712452AD853D80014A2BF /* CollapsibleBannerViewController.swift */; };
3942
/* End PBXBuildFile section */
4043

@@ -70,6 +73,9 @@
7073
AED11118203213F500EA4BEE /* UnifiedNativeAdView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = UnifiedNativeAdView.xib; sourceTree = "<group>"; };
7174
AED111252032151200EA4BEE /* SimpleNativeAdView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SimpleNativeAdView.swift; sourceTree = "<group>"; };
7275
AED111272032174100EA4BEE /* CustomControlsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomControlsView.swift; sourceTree = "<group>"; };
76+
EF25D0272E4B97F000688F43 /* BannerSnippets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BannerSnippets.swift; sourceTree = "<group>"; };
77+
EF25D0292E4B97FA00688F43 /* ResponseInfoSnippets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ResponseInfoSnippets.swift; sourceTree = "<group>"; };
78+
EF25D02B2E4B980100688F43 /* RewardedInterstitialAdSnippets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RewardedInterstitialAdSnippets.swift; sourceTree = "<group>"; };
7379
EFC712452AD853D80014A2BF /* CollapsibleBannerViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollapsibleBannerViewController.swift; sourceTree = "<group>"; };
7480
/* End PBXFileReference section */
7581

@@ -140,6 +146,9 @@
140146
A8B650372DD63E0F00AE1D3F /* Snippets */ = {
141147
isa = PBXGroup;
142148
children = (
149+
EF25D02B2E4B980100688F43 /* RewardedInterstitialAdSnippets.swift */,
150+
EF25D0292E4B97FA00688F43 /* ResponseInfoSnippets.swift */,
151+
EF25D0272E4B97F000688F43 /* BannerSnippets.swift */,
143152
A86568AA2E254CD100D360C3 /* ATTSnippets.swift */,
144153
A8265F6C2E4F882600A9C150 /* BannerSnippets.swift */,
145154
A8265F702E4F884100A9C150 /* NativeAdOptionsSnippets.swift */,
@@ -258,14 +267,14 @@
258267
A8265F6F2E4F883500A9C150 /* ResponseInfoSnippets.swift in Sources */,
259268
507818F8219A418100E5A44A /* AdManagerCustomVideoControlsController.swift in Sources */,
260269
507818FA219A418100E5A44A /* AdManagerFluidAdSizeViewController.swift in Sources */,
261-
A8265F6D2E4F882600A9C150 /* BannerSnippets.swift in Sources */,
262270
A86568AB2E254CD500D360C3 /* ATTSnippets.swift in Sources */,
263271
4A7A6CD71C76237500FB1A32 /* Constants.swift in Sources */,
264272
507818F6219A418100E5A44A /* AdManagerPPIDViewController.swift in Sources */,
265273
A846F1C12E2583D200F96E49 /* RewardedAdSnippets.swift in Sources */,
266274
EFC712462AD853D80014A2BF /* CollapsibleBannerViewController.swift in Sources */,
267275
AED111282032174100EA4BEE /* CustomControlsView.swift in Sources */,
268276
A8B650362DD63E0300AE1D3F /* RequestConfigurationSnippets.swift in Sources */,
277+
EF25D02C2E4B980C00688F43 /* RewardedInterstitialAdSnippets.swift in Sources */,
269278
4AA7D6AE1C626F2D00DFD2EB /* AdMobBannerSizesViewController.swift in Sources */,
270279
AE3068CA21221F7E006D7843 /* AdMobNativeCustomMuteThisAdViewController.swift in Sources */,
271280
507818F4219A418100E5A44A /* AdManagerMultipleAdSizesViewController.swift in Sources */,
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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 GoogleMobileAds
18+
19+
private class RewardedInterstitialAdSnippets: UIViewController, FullScreenContentDelegate {
20+
21+
private var rewardedInterstitialAd: RewardedInterstitialAd?
22+
23+
// Replace this ad unit ID with your own ad unit ID.
24+
private let adUnitID = "ca-app-pub-3940256099942544/6978759866"
25+
private let adManagerAdUnitID = "/21775744923/example/rewarded-interstitial"
26+
27+
// [START load_ad]
28+
func loadRewardedInterstitialAd() async {
29+
do {
30+
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
31+
// Replace this ad unit ID with your own ad unit ID.
32+
with: adUnitID, request: Request())
33+
// [START set_delegate]
34+
rewardedInterstitialAd?.fullScreenContentDelegate = self
35+
// [END set_delegate]
36+
} catch {
37+
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
38+
}
39+
}
40+
// [END load_ad]
41+
42+
// [START load_ad_gam]
43+
func loadAdManagerRewardedInterstitialAd() async {
44+
do {
45+
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
46+
// Replace this ad unit ID with your own ad unit ID.
47+
with: adUnitID, request: AdManagerRequest())
48+
rewardedInterstitialAd?.fullScreenContentDelegate = self
49+
} catch {
50+
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
51+
}
52+
}
53+
// [END load_ad_gam]
54+
55+
// [START validate_server_side_verification]
56+
private func validateServerSideVerification() async {
57+
do {
58+
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
59+
// Replace this ad unit ID with your own ad unit ID.
60+
with: adUnitID, request: Request())
61+
let options = ServerSideVerificationOptions()
62+
options.customRewardText = "SAMPLE_CUSTOM_DATA_STRING"
63+
rewardedInterstitialAd?.serverSideVerificationOptions = options
64+
} catch {
65+
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
66+
}
67+
}
68+
// [END validate_server_side_verification]
69+
70+
// [START validate_gam_server_side_verification]
71+
private func validateAdManagerServerSideVerification() async {
72+
do {
73+
rewardedInterstitialAd = try await RewardedInterstitialAd.load(
74+
// Replace this ad unit ID with your own ad unit ID.
75+
with: adUnitID, request: AdManagerRequest())
76+
let options = ServerSideVerificationOptions()
77+
options.customRewardText = "SAMPLE_CUSTOM_DATA_STRING"
78+
rewardedInterstitialAd?.serverSideVerificationOptions = options
79+
} catch {
80+
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
81+
}
82+
}
83+
// [END validate_gam_server_side_verification]
84+
85+
// [START ad_events]
86+
func adDidRecordImpression(_ ad: FullScreenPresentingAd) {
87+
print("\(#function) called.")
88+
}
89+
90+
func adDidRecordClick(_ ad: FullScreenPresentingAd) {
91+
print("\(#function) called.")
92+
}
93+
94+
func adWillPresentFullScreenContent(_ ad: FullScreenPresentingAd) {
95+
print("\(#function) called.")
96+
}
97+
98+
func adWillDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
99+
print("\(#function) called.")
100+
}
101+
102+
func adDidDismissFullScreenContent(_ ad: FullScreenPresentingAd) {
103+
print("\(#function) called.")
104+
// Clear the rewarded interstitial ad.
105+
rewardedInterstitialAd = nil
106+
}
107+
108+
func ad(
109+
_ ad: FullScreenPresentingAd,
110+
didFailToPresentFullScreenContentWithError error: Error
111+
) {
112+
print("\(#function) called with error: \(error.localizedDescription).")
113+
}
114+
// [END ad_events]
115+
116+
// [START show_ad]
117+
func showRewardedInterstitialAd() {
118+
guard let rewardedInterstitialAd = rewardedInterstitialAd else {
119+
return print("Ad wasn't ready.")
120+
}
121+
122+
// The UIViewController parameter is an optional.
123+
rewardedInterstitialAd.present(from: nil) {
124+
let reward = rewardedInterstitialAd.adReward
125+
print("Reward received with currency \(reward.amount), amount \(reward.amount.doubleValue)")
126+
// TODO: Reward the user.
127+
}
128+
}
129+
// [END show_ad]
130+
}

0 commit comments

Comments
 (0)