Skip to content

Commit 23bd62f

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Added snippets for server to server requests.
PiperOrigin-RevId: 805096286
1 parent 90e2ffa commit 23bd62f

File tree

4 files changed

+387
-24
lines changed

4 files changed

+387
-24
lines changed

Objective-C/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.m

Lines changed: 118 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#import <GoogleMobileAds/GoogleMobileAds.h>
1818
#import <UIKit/UIKit.h>
1919

20-
@interface AdManagerSCARSnippets : NSObject
20+
@interface AdManagerSCARSnippets : NSObject <GADNativeAdLoaderDelegate, GAMBannerAdLoaderDelegate>
21+
@property(nonatomic, strong) GADAdLoader *adLoader;
22+
2123
- (void)loadNative:(NSString *)adUnitID;
2224
- (void)loadBanner:(NSString *)adUnitID;
2325
- (void)loadNativePlusBanner:(NSString *)adUnitID;
@@ -46,7 +48,8 @@ - (void)loadNative:(NSString *)adUnitID {
4648
return;
4749
}
4850
NSLog(@"Signal string: %@", signal.signalString);
49-
// TODO: Fetch the ad response using your generated signal.
51+
// Fetch the ad response using your generated signal.
52+
NSString *adResponseString = [self fetchSignalResponse:signal];
5053
}];
5154
// [END signal_request_native]
5255
}
@@ -74,7 +77,8 @@ - (void)loadBanner:(NSString *)adUnitID {
7477
return;
7578
}
7679
NSLog(@"Signal string: %@", signal.signalString);
77-
// TODO: Fetch the ad response using your generated signal.
80+
// Fetch the ad response using your generated signal.
81+
NSString *adResponseString = [self fetchSignalResponse:signal];
7882
}];
7983
// [END signal_request_banner]
8084
}
@@ -86,12 +90,13 @@ - (void)loadNativePlusBanner:(NSString *)adUnitID {
8690
// denote that the usage of QueryInfo is for Ad Manager S2S.
8791
GADNativeSignalRequest *signalRequest =
8892
[[GADNativeSignalRequest alloc] initWithSignalType:@"signal_type_ad_manager_s2s"];
89-
signalRequest.requestAgent = @"request_agent";
93+
signalRequest.requestAgent = @"REQUEST_AGENT";
9094
signalRequest.adUnitID = adUnitID;
9195
signalRequest.adLoaderAdTypes =
9296
[NSSet setWithArray:@[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]];
9397
// Refer to the AdSize class for available ad sizes.
94-
signalRequest.adSizes = @[ @(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(375)) ];
98+
signalRequest.adSizes =
99+
@[ NSValueFromGADAdSize(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(375)) ];
95100

96101
[GADMobileAds generateSignal:signalRequest
97102
completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) {
@@ -104,9 +109,116 @@ - (void)loadNativePlusBanner:(NSString *)adUnitID {
104109
return;
105110
}
106111
NSLog(@"Signal string: %@", signal.signalString);
107-
// TODO: Fetch the ad response using your generated signal.
112+
// Fetch the ad response using your generated signal.
113+
NSString *adResponseString = [self fetchSignalResponse:signal];
108114
}];
109115
// [END signal_request_native_plus_banner]
110116
}
111117

118+
- (void)loadNativeWithOptions:(NSString *)adUnitID {
119+
// [START native_ad_options]
120+
// Create a signal request for an ad.
121+
// Specify the "signal_type_ad_manager_s2s" to
122+
// denote that the usage of QueryInfo is for Ad Manager S2S.
123+
GADNativeSignalRequest *signalRequest =
124+
[[GADNativeSignalRequest alloc] initWithSignalType:@"signal_type_ad_manager_s2s"];
125+
signalRequest.requestAgent = @"REQUEST_AGENT";
126+
signalRequest.adUnitID = adUnitID;
127+
128+
// Enable shared native ad options.
129+
signalRequest.disableImageLoading = NO;
130+
signalRequest.mediaAspectRatio = GADMediaAspectRatioAny;
131+
signalRequest.shouldRequestMultipleImages = YES;
132+
signalRequest.preferredAdChoicesPosition = GADAdChoicesPositionTopRightCorner;
133+
134+
// Enable video options.
135+
GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
136+
videoOptions.startMuted = YES;
137+
signalRequest.videoOptions = videoOptions;
138+
139+
[GADMobileAds generateSignal:signalRequest
140+
completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) {
141+
if (error != nil) {
142+
NSLog(@"Error getting ad info: %@", error.localizedDescription);
143+
return;
144+
}
145+
if (signal == nil) {
146+
NSLog(@"Unexpected error - query info is nil.");
147+
return;
148+
}
149+
NSLog(@"Signal string: %@", signal.signalString);
150+
// Fetch the ad response using your generated signal.
151+
NSString *adResponseString = [self fetchSignalResponse:signal];
152+
}];
153+
// [END native_ad_options]
154+
}
155+
156+
// [START fetch_response]
157+
// This function emulates a request to your ad server.
158+
- (NSString *)fetchSignalResponse:(GADSignal *)signal {
159+
return @"adResponseString";
160+
}
161+
// [END fetch_response]
162+
163+
- (void)renderBanner:(NSString *)adUnitID
164+
adResponseString:(NSString *)adResponseString
165+
rootViewController:(UIViewController *)rootViewController {
166+
// [START render_banner]
167+
self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:adUnitID
168+
rootViewController:rootViewController
169+
adTypes:@[ GADAdLoaderAdTypeGAMBanner ]
170+
options:nil];
171+
self.adLoader.delegate = self;
172+
[self.adLoader loadWithAdResponseString:adResponseString];
173+
// [END render_banner]
174+
}
175+
176+
- (void)renderNative:(NSString *)adUnitID
177+
adResponseString:(NSString *)adResponseString
178+
rootViewController:(UIViewController *)rootViewController {
179+
// [START render_native]
180+
self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:adUnitID
181+
rootViewController:rootViewController
182+
adTypes:@[ GADAdLoaderAdTypeNative ]
183+
options:nil];
184+
self.adLoader.delegate = self;
185+
[self.adLoader loadWithAdResponseString:adResponseString];
186+
// [END render_native]
187+
}
188+
189+
- (void)renderNativePlusBanner:(NSString *)adUnitID
190+
adResponseString:(NSString *)adResponseString
191+
rootViewController:(UIViewController *)rootViewController {
192+
// [START render_native_plus_banner]
193+
self.adLoader =
194+
[[GADAdLoader alloc] initWithAdUnitID:adUnitID
195+
rootViewController:rootViewController
196+
adTypes:@[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]
197+
options:nil];
198+
self.adLoader.delegate = self;
199+
[self.adLoader loadWithAdResponseString:adResponseString];
200+
// [END render_native_plus_banner]
201+
}
202+
203+
- (nonnull NSArray<NSValue *> *)validBannerSizesForAdLoader:(nonnull GADAdLoader *)adLoader {
204+
return @[ NSValueFromGADAdSize(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(375)) ];
205+
}
206+
207+
#pragma mark - GADNativeAdLoaderDelegate
208+
209+
- (void)adLoader:(nonnull GADAdLoader *)adLoader
210+
didReceiveGAMBannerView:(nonnull GAMBannerView *)bannerView {
211+
// GAM Banner ad received.
212+
}
213+
214+
- (void)adLoader:(nonnull GADAdLoader *)adLoader
215+
didReceiveNativeAd:(nonnull GADNativeAd *)nativeAd {
216+
// Native ad received.
217+
}
218+
219+
- (void)adLoader:(nonnull GADAdLoader *)adLoader
220+
didFailToReceiveAdWithError:(nonnull NSError *)error {
221+
// Native ad failed to load.
222+
}
223+
112224
@end

Objective-C/advanced/APIDemo/APIDemo/Snippets/AdMobSCARSnippets.m

Lines changed: 82 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
#import <GoogleMobileAds/GoogleMobileAds.h>
1818
#import <UIKit/UIKit.h>
1919

20-
@interface AdMobSCARSnippets : NSObject
20+
@interface AdMobSCARSnippets : NSObject <GADNativeAdLoaderDelegate>
21+
@property(nonatomic, strong) GADAdLoader *adLoader;
22+
2123
- (void)loadNative:(NSString *)adUnitID;
2224
- (void)loadBanner:(NSString *)adUnitID;
2325
@end
@@ -43,7 +45,8 @@ - (void)loadNative:(NSString *)adUnitID {
4345
return;
4446
}
4547
NSLog(@"Signal string: %@", signal.signalString);
46-
// TODO: Fetch the ad response using your generated signal.
48+
// Fetch the ad response using your generated signal.
49+
NSString *adResponseString = [self fetchSignalResponse:signal];
4750
}];
4851
// [END signal_request_native]
4952
}
@@ -66,13 +69,88 @@ - (void)loadBanner:(NSString *)adUnitID {
6669
return;
6770
}
6871
if (signal == nil) {
69-
NSLog(@"Unexpected error - Signal is nil.");
72+
NSLog(@"Unexpected error - signal is nil.");
7073
return;
7174
}
7275
NSLog(@"Signal string: %@", signal.signalString);
73-
// TODO: Fetch the ad response using your generated signal.
76+
// Fetch the ad response using your generated signal.
77+
NSString *adResponseString = [self fetchSignalResponse:signal];
7478
}];
7579
// [END signal_request_banner]
7680
}
7781

82+
- (void)loadNativeWithOptions:(NSString *)adUnitID {
83+
// [START native_ad_options]
84+
// Create a signal request for an ad.
85+
GADNativeSignalRequest *signalRequest =
86+
[[GADNativeSignalRequest alloc] initWithSignalType:@"SIGNAL_TYPE"];
87+
signalRequest.requestAgent = @"REQUEST_AGENT";
88+
signalRequest.adUnitID = adUnitID;
89+
90+
// Enable shared native ad options.
91+
signalRequest.disableImageLoading = NO;
92+
signalRequest.mediaAspectRatio = GADMediaAspectRatioAny;
93+
signalRequest.shouldRequestMultipleImages = YES;
94+
signalRequest.preferredAdChoicesPosition = GADAdChoicesPositionTopRightCorner;
95+
96+
// Enable video options.
97+
GADVideoOptions *videoOptions = [[GADVideoOptions alloc] init];
98+
videoOptions.startMuted = YES;
99+
signalRequest.videoOptions = videoOptions;
100+
101+
[GADMobileAds generateSignal:signalRequest
102+
completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) {
103+
if (error != nil) {
104+
NSLog(@"Error getting ad info: %@", error.localizedDescription);
105+
return;
106+
}
107+
if (signal == nil) {
108+
NSLog(@"Unexpected error - signal is nil.");
109+
return;
110+
}
111+
NSLog(@"Signal string: %@", signal.signalString);
112+
// Fetch the ad response using your generated signal.
113+
NSString *adResponseString = [self fetchSignalResponse:signal];
114+
}];
115+
// [END native_ad_options]
116+
}
117+
118+
// [START fetch_response]
119+
// This function emulates a request to your ad server.
120+
- (NSString *)fetchSignalResponse:(GADSignal *)signal {
121+
return @"adResponseString";
122+
}
123+
// [END fetch_response]
124+
125+
- (void)renderBanner:(NSString *)adUnitID adResponseString:(NSString *)adResponseString {
126+
// [START render_banner]
127+
GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeBanner];
128+
bannerView.adUnitID = adUnitID;
129+
[bannerView loadWithAdResponseString:adResponseString];
130+
// [END render_banner]
131+
}
132+
133+
- (void)renderNative:(NSString *)adUnitID
134+
adResponseString:(NSString *)adResponseString
135+
rootViewController:(UIViewController *)rootViewController {
136+
// [START render_native]
137+
self.adLoader = [[GADAdLoader alloc] initWithAdUnitID:adUnitID
138+
rootViewController:rootViewController
139+
adTypes:@[ GADAdLoaderAdTypeNative ]
140+
options:nil];
141+
self.adLoader.delegate = self;
142+
[self.adLoader loadWithAdResponseString:adResponseString];
143+
// [END render_native]
144+
}
145+
146+
#pragma mark - GADNativeAdLoaderDelegate
147+
148+
- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {
149+
// Native ad received.
150+
}
151+
152+
- (void)adLoader:(GADAdLoader *)adLoader didFailToReceiveAdWithError:(NSError *)error {
153+
// Native ad failed to load.
154+
}
155+
78156
@end

0 commit comments

Comments
 (0)