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