Skip to content

Commit bcd8c23

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

File tree

4 files changed

+376
-26
lines changed

4 files changed

+376
-26
lines changed

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

Lines changed: 113 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,12 @@ - (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";
9093
signalRequest.adUnitID = adUnitID;
9194
signalRequest.adLoaderAdTypes =
9295
[NSSet setWithArray:@[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]];
9396
// Refer to the AdSize class for available ad sizes.
94-
signalRequest.adSizes = @[ @(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(375)) ];
97+
signalRequest.adSizes =
98+
@[ NSValueFromGADAdSize(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth(375)) ];
9599

96100
[GADMobileAds generateSignal:signalRequest
97101
completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) {
@@ -104,9 +108,112 @@ - (void)loadNativePlusBanner:(NSString *)adUnitID {
104108
return;
105109
}
106110
NSLog(@"Signal string: %@", signal.signalString);
107-
// TODO: Fetch the ad response using your generated signal.
111+
// Fetch the ad response using your generated signal.
112+
NSString *adResponseString = [self fetchSignalResponse:signal];
108113
}];
109114
// [END signal_request_native_plus_banner]
110115
}
111116

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

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

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

0 commit comments

Comments
 (0)