1414// limitations under the License.
1515//
1616
17- #import < GoogleMobileAds/GoogleMobileAds.h>
1817#import < UIKit/UIKit.h>
18+ #import < GoogleMobileAds/GoogleMobileAds.h>
19+
20+ // / A class demonstrating how to use the Google Mobile Ads SDK for SCAR requests.
21+ @interface AdManagerSCARSnippets : NSObject <GADNativeAdLoaderDelegate, GAMBannerAdLoaderDelegate>
22+ @property (nonatomic , strong ) GADAdLoader *adLoader;
1923
20- @interface AdManagerSCARSnippets : NSObject
21- - (void )loadNative : (NSString *)adUnitID ;
22- - (void )loadBanner : (NSString *)adUnitID ;
23- - (void )loadNativePlusBanner : (NSString *)adUnitID ;
24+ - (void )loadNative : (NSString *)adUnitID rootViewController : (UIViewController *)rootViewController ;
25+ - (void )loadBanner : (NSString *)adUnitID rootViewController : (UIViewController *)rootViewController ;
26+ - (void )loadNativePlusBanner : (NSString *)adUnitID
27+ rootViewController : (UIViewController *)rootViewController ;
28+ - (void )loadNativeWithOptions : (NSString *)adUnitID
29+ rootViewController : (UIViewController *)rootViewController ;
2430@end
2531
2632@implementation AdManagerSCARSnippets
2733
28- - (void )loadNative : (NSString *)adUnitID {
34+ - (void )loadNative : (NSString *)adUnitID rootViewController : (UIViewController *) rootViewController {
2935 // [START signal_request_native]
3036 // Create a signal request for an ad.
31- // Specify the "signal_type_ad_manager_s2s" to
32- // denote that the usage of QueryInfo is for Ad Manager S2S.
37+ // Specify the "signal_type_ad_manager_s2s" to denote that the usage of @c GADSignal is for Ad
38+ // Manager S2S.
3339 GADNativeSignalRequest *signalRequest =
3440 [[GADNativeSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
3541 signalRequest.requestAgent = @" REQUEST_AGENT" ;
@@ -38,75 +44,199 @@ - (void)loadNative:(NSString *)adUnitID {
3844 [GADMobileAds generateSignal: signalRequest
3945 completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
4046 if (error != nil ) {
41- NSLog (@" Error getting ad info : %@ " , error.localizedDescription );
47+ NSLog (@" Error getting signal : %@ " , error.localizedDescription );
4248 return ;
4349 }
4450 if (signal == nil ) {
45- NSLog (@" Unexpected error - query info is nil." );
51+ NSLog (@" Unexpected error - signal is nil." );
4652 return ;
4753 }
4854 NSLog (@" Signal string: %@ " , signal.signalString );
49- // TODO: Fetch the ad response using your generated signal.
55+ // Fetch the ad response using your generated signal.
56+ NSString *adResponseString = [self fetchAdResponseString: signal];
57+ [self renderNative: adUnitID
58+ adResponseString: adResponseString
59+ rootViewController: rootViewController];
5060 }];
5161 // [END signal_request_native]
5262}
5363
54- - (void )loadBanner : (NSString *)adUnitID {
64+ - (void )loadBanner : (NSString *)adUnitID rootViewController : (UIViewController *) rootViewController {
5565 // [START signal_request_banner]
5666 // Create a signal request for an ad.
5767 // Specify the "signal_type_ad_manager_s2s" to
58- // denote that the usage of QueryInfo is for Ad Manager S2S.
68+ // denote that the usage of @c GADSignal is for Ad Manager S2S.
5969 GADBannerSignalRequest *signalRequest =
6070 [[GADBannerSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
6171 signalRequest.requestAgent = @" REQUEST_AGENT" ;
6272 signalRequest.adUnitID = adUnitID;
63- // Refer to the AdSize class for available ad sizes.
73+ // Refer to the @c AdSize class for available ad sizes.
6474 signalRequest.adSize = GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 );
6575
6676 [GADMobileAds generateSignal: signalRequest
6777 completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
6878 if (error != nil ) {
69- NSLog (@" Error getting ad info : %@ " , error.localizedDescription );
79+ NSLog (@" Error getting signal : %@ " , error.localizedDescription );
7080 return ;
7181 }
7282 if (signal == nil ) {
73- NSLog (@" Unexpected error - query info is nil." );
83+ NSLog (@" Unexpected error - signal is nil." );
7484 return ;
7585 }
7686 NSLog (@" Signal string: %@ " , signal.signalString );
77- // TODO: Fetch the ad response using your generated signal.
87+ // Fetch the ad response using your generated signal.
88+ NSString *adResponseString = [self fetchAdResponseString: signal];
89+ [self renderBanner: adUnitID
90+ adResponseString: adResponseString
91+ rootViewController: rootViewController];
7892 }];
7993 // [END signal_request_banner]
8094}
8195
82- - (void )loadNativePlusBanner : (NSString *)adUnitID {
96+ - (void )loadNativePlusBanner : (NSString *)adUnitID
97+ rootViewController : (UIViewController *)rootViewController {
8398 // [START signal_request_native_plus_banner]
8499 // Create a signal request for an ad.
85- // Specify the "signal_type_ad_manager_s2s" to
86- // denote that the usage of QueryInfo is for Ad Manager S2S.
100+ // Specify the "signal_type_ad_manager_s2s" to denote that the usage of @c GADSignal is for Ad
101+ // Manager S2S.
87102 GADNativeSignalRequest *signalRequest =
88103 [[GADNativeSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
89- signalRequest.requestAgent = @" request_agent" ;
90104 signalRequest.adUnitID = adUnitID;
91105 signalRequest.adLoaderAdTypes =
92106 [NSSet setWithArray: @[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]];
93- // Refer to the AdSize class for available ad sizes.
94- signalRequest.adSizes = @[ @(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 )) ];
107+ // Refer to the @c AdSize class for available ad sizes.
108+ signalRequest.adSizes =
109+ @[ NSValueFromGADAdSize(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 )) ];
95110
96111 [GADMobileAds generateSignal: signalRequest
97112 completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
98113 if (error != nil ) {
99- NSLog (@" Error getting ad info : %@ " , error.localizedDescription );
114+ NSLog (@" Error getting signal : %@ " , error.localizedDescription );
100115 return ;
101116 }
102117 if (signal == nil ) {
103- NSLog (@" Unexpected error - query info is nil." );
118+ NSLog (@" Unexpected error - signal is nil." );
104119 return ;
105120 }
106121 NSLog (@" Signal string: %@ " , signal.signalString );
107- // TODO: Fetch the ad response using your generated signal.
122+ // Fetch the ad response using your generated signal.
123+ NSString *adResponseString = [self fetchAdResponseString: signal];
124+ [self renderNativePlusBanner: adUnitID
125+ adResponseString: adResponseString
126+ rootViewController: rootViewController];
108127 }];
109128 // [END signal_request_native_plus_banner]
110129}
111130
131+ - (void )loadNativeWithOptions : (NSString *)adUnitID
132+ rootViewController : (UIViewController *)rootViewController {
133+ // [START native_ad_options]
134+ // Create a signal request for an ad.
135+ // Specify the "signal_type_ad_manager_s2s" to denote that the usage of @c GADSignal is for Ad
136+ // Manager S2S.
137+ GADNativeSignalRequest *signalRequest =
138+ [[GADNativeSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
139+ signalRequest.requestAgent = @" REQUEST_AGENT" ;
140+ signalRequest.adUnitID = adUnitID;
141+
142+ // Enable shared native ad options.
143+ signalRequest.disableImageLoading = NO ;
144+ signalRequest.mediaAspectRatio = GADMediaAspectRatioAny;
145+ signalRequest.preferredAdChoicesPosition = GADAdChoicesPositionTopRightCorner;
146+
147+ // Enable video options.
148+ GADVideoOptions *videoOptions = [[GADVideoOptions alloc ] init ];
149+ videoOptions.startMuted = YES ;
150+ signalRequest.videoOptions = videoOptions;
151+
152+ [GADMobileAds generateSignal: signalRequest
153+ completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
154+ if (error != nil ) {
155+ NSLog (@" Error getting signal: %@ " , error.localizedDescription );
156+ return ;
157+ }
158+ if (signal == nil ) {
159+ NSLog (@" Unexpected error - signal is nil." );
160+ return ;
161+ }
162+ NSLog (@" Signal string: %@ " , signal.signalString );
163+ // Fetch the ad response using your generated signal.
164+ NSString *adResponseString = [self fetchAdResponseString: signal];
165+ [self renderNative: adUnitID
166+ adResponseString: adResponseString
167+ rootViewController: rootViewController];
168+ }];
169+ // [END native_ad_options]
170+ }
171+
172+ // [START fetch_response]
173+ // Emulates a request to your ad server.
174+ - (NSString *)fetchAdResponseString : (GADSignal *)signal {
175+ return @" adResponseString" ;
176+ }
177+ // [END fetch_response]
178+
179+ - (void )renderBanner : (NSString *)adUnitID
180+ adResponseString : (NSString *)adResponseString
181+ rootViewController : (UIViewController *)rootViewController {
182+ // [START render_banner]
183+ self.adLoader = [[GADAdLoader alloc ] initWithAdUnitID: adUnitID
184+ rootViewController: rootViewController
185+ adTypes: @[ GADAdLoaderAdTypeGAMBanner ]
186+ options: nil ];
187+ self.adLoader .delegate = self;
188+ [self .adLoader loadWithAdResponseString: adResponseString];
189+ // [END render_banner]
190+ }
191+
192+ - (void )renderNative : (NSString *)adUnitID
193+ adResponseString : (NSString *)adResponseString
194+ rootViewController : (UIViewController *)rootViewController {
195+ // [START render_native]
196+ self.adLoader = [[GADAdLoader alloc ] initWithAdUnitID: adUnitID
197+ rootViewController: rootViewController
198+ adTypes: @[ GADAdLoaderAdTypeNative ]
199+ options: nil ];
200+ self.adLoader .delegate = self;
201+ [self .adLoader loadWithAdResponseString: adResponseString];
202+ // [END render_native]
203+ }
204+
205+ - (void )renderNativePlusBanner : (NSString *)adUnitID
206+ adResponseString : (NSString *)adResponseString
207+ rootViewController : (UIViewController *)rootViewController {
208+ // [START render_native_plus_banner]
209+ self.adLoader =
210+ [[GADAdLoader alloc ] initWithAdUnitID: adUnitID
211+ rootViewController: rootViewController
212+ adTypes: @[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]
213+ options: nil ];
214+ self.adLoader .delegate = self;
215+ [self .adLoader loadWithAdResponseString: adResponseString];
216+ // [END render_native_plus_banner]
217+ }
218+
219+ #pragma mark - GAMBannerAdLoaderDelegate
220+
221+ - (void )adLoader : (nonnull GADAdLoader *)adLoader
222+ didReceiveGAMBannerView : (nonnull GAMBannerView *)bannerView {
223+ // GAM Banner ad received.
224+ }
225+
226+ - (nonnull NSArray <NSValue *> *)validGAMBannerSizesForAdLoader : (nonnull GADAdLoader *)adLoader {
227+ return @[ NSValueFromGADAdSize(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 )) ];
228+ }
229+
230+ #pragma mark - GADNativeAdLoaderDelegate
231+
232+ - (void )adLoader : (nonnull GADAdLoader *)adLoader
233+ didReceiveNativeAd : (nonnull GADNativeAd *)nativeAd {
234+ // Native ad received.
235+ }
236+
237+ - (void )adLoader : (nonnull GADAdLoader *)adLoader
238+ didFailToReceiveAdWithError : (nonnull NSError *)error {
239+ // Native ad failed to load.
240+ }
241+
112242@end
0 commit comments