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
0 commit comments