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,
22+ GAMBannerAdLoaderDelegate,
23+ GADBannerViewDelegate>
24+ @property (nonatomic , strong ) GADAdLoader *adLoader;
25+ @property (nonatomic , strong ) GAMBannerView *bannerView;
1926
20- @interface AdManagerSCARSnippets : NSObject
21- - (void )loadNative : (NSString *)adUnitID ;
22- - (void )loadBanner : (NSString *)adUnitID ;
23- - (void )loadNativePlusBanner : (NSString *)adUnitID ;
27+ - (void )loadNative : (NSString *)adUnitID rootViewController : (UIViewController *)rootViewController ;
28+ - (void )loadBanner : (NSString *)adUnitID rootViewController : (UIViewController *)rootViewController ;
29+ - (void )loadNativePlusBanner : (NSString *)adUnitID
30+ rootViewController : (UIViewController *)rootViewController ;
31+ - (void )loadNativeWithOptions : (NSString *)adUnitID
32+ rootViewController : (UIViewController *)rootViewController ;
2433@end
2534
2635@implementation AdManagerSCARSnippets
2736
28- - (void )loadNative : (NSString *)adUnitID {
37+ - (void )loadNative : (NSString *)adUnitID rootViewController : (UIViewController *) rootViewController {
2938 // [START signal_request_native]
3039 // 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.
40+ // Specify the "signal_type_ad_manager_s2s" to denote that the usage of @c GADSignal is for Ad
41+ // Manager S2S.
3342 GADNativeSignalRequest *signalRequest =
3443 [[GADNativeSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
3544 signalRequest.requestAgent = @" REQUEST_AGENT" ;
@@ -38,75 +47,216 @@ - (void)loadNative:(NSString *)adUnitID {
3847 [GADMobileAds generateSignal: signalRequest
3948 completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
4049 if (error != nil ) {
41- NSLog (@" Error getting ad info : %@ " , error.localizedDescription );
50+ NSLog (@" Error getting signal : %@ " , error.localizedDescription );
4251 return ;
4352 }
4453 if (signal == nil ) {
45- NSLog (@" Unexpected error - query info is nil." );
54+ NSLog (@" Unexpected error - signal is nil." );
4655 return ;
4756 }
4857 NSLog (@" Signal string: %@ " , signal.signalString );
49- // TODO: Fetch the ad response using your generated signal.
58+ // Fetch the ad response using your generated signal.
59+ NSString *adResponseString = [self fetchAdResponseString: signal];
60+ [self renderNative: adUnitID
61+ adResponseString: adResponseString
62+ rootViewController: rootViewController];
5063 }];
5164 // [END signal_request_native]
5265}
5366
54- - (void )loadBanner : (NSString *)adUnitID {
67+ - (void )loadBanner : (NSString *)adUnitID rootViewController : (UIViewController *) rootViewController {
5568 // [START signal_request_banner]
5669 // Create a signal request for an ad.
5770 // Specify the "signal_type_ad_manager_s2s" to
58- // denote that the usage of QueryInfo is for Ad Manager S2S.
71+ // denote that the usage of @c GADSignal is for Ad Manager S2S.
5972 GADBannerSignalRequest *signalRequest =
6073 [[GADBannerSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
6174 signalRequest.requestAgent = @" REQUEST_AGENT" ;
6275 signalRequest.adUnitID = adUnitID;
63- // Refer to the AdSize class for available ad sizes.
76+ // Refer to the @c AdSize class for available ad sizes.
6477 signalRequest.adSize = GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 );
6578
6679 [GADMobileAds generateSignal: signalRequest
6780 completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
6881 if (error != nil ) {
69- NSLog (@" Error getting ad info : %@ " , error.localizedDescription );
82+ NSLog (@" Error getting signal : %@ " , error.localizedDescription );
7083 return ;
7184 }
7285 if (signal == nil ) {
73- NSLog (@" Unexpected error - query info is nil." );
86+ NSLog (@" Unexpected error - signal is nil." );
7487 return ;
7588 }
7689 NSLog (@" Signal string: %@ " , signal.signalString );
77- // TODO: Fetch the ad response using your generated signal.
90+ // Fetch the ad response using your generated signal.
91+ NSString *adResponseString = [self fetchAdResponseString: signal];
92+ [self renderBanner: adUnitID
93+ adResponseString: adResponseString
94+ rootViewController: rootViewController];
7895 }];
7996 // [END signal_request_banner]
8097}
8198
82- - (void )loadNativePlusBanner : (NSString *)adUnitID {
99+ - (void )loadNativePlusBanner : (NSString *)adUnitID
100+ rootViewController : (UIViewController *)rootViewController {
83101 // [START signal_request_native_plus_banner]
84102 // 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.
103+ // Specify the "signal_type_ad_manager_s2s" to denote that the usage of @c GADSignal is for Ad
104+ // Manager S2S.
87105 GADNativeSignalRequest *signalRequest =
88106 [[GADNativeSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
89- signalRequest.requestAgent = @" request_agent" ;
90107 signalRequest.adUnitID = adUnitID;
91108 signalRequest.adLoaderAdTypes =
92109 [NSSet setWithArray: @[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]];
93- // Refer to the AdSize class for available ad sizes.
94- signalRequest.adSizes = @[ @(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 )) ];
110+ // Refer to the @c AdSize class for available ad sizes.
111+ signalRequest.adSizes =
112+ @[ NSValueFromGADAdSize(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 )) ];
95113
96114 [GADMobileAds generateSignal: signalRequest
97115 completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
98116 if (error != nil ) {
99- NSLog (@" Error getting ad info : %@ " , error.localizedDescription );
117+ NSLog (@" Error getting signal : %@ " , error.localizedDescription );
100118 return ;
101119 }
102120 if (signal == nil ) {
103- NSLog (@" Unexpected error - query info is nil." );
121+ NSLog (@" Unexpected error - signal is nil." );
104122 return ;
105123 }
106124 NSLog (@" Signal string: %@ " , signal.signalString );
107- // TODO: Fetch the ad response using your generated signal.
125+ // Fetch the ad response using your generated signal.
126+ NSString *adResponseString = [self fetchAdResponseString: signal];
127+ [self renderNativePlusBanner: adUnitID
128+ adResponseString: adResponseString
129+ rootViewController: rootViewController];
108130 }];
109131 // [END signal_request_native_plus_banner]
110132}
111133
134+ - (void )loadNativeWithOptions : (NSString *)adUnitID
135+ rootViewController : (UIViewController *)rootViewController {
136+ // [START native_ad_options]
137+ // Create a signal request for an ad.
138+ // Specify the "signal_type_ad_manager_s2s" to denote that the usage of @c GADSignal is for Ad
139+ // Manager S2S.
140+ GADNativeSignalRequest *signalRequest =
141+ [[GADNativeSignalRequest alloc ] initWithSignalType: @" signal_type_ad_manager_s2s" ];
142+ signalRequest.requestAgent = @" REQUEST_AGENT" ;
143+ signalRequest.adUnitID = adUnitID;
144+
145+ // Enable shared native ad options.
146+ signalRequest.disableImageLoading = NO ;
147+ signalRequest.mediaAspectRatio = GADMediaAspectRatioAny;
148+ signalRequest.preferredAdChoicesPosition = GADAdChoicesPositionTopRightCorner;
149+
150+ // Enable video options.
151+ GADVideoOptions *videoOptions = [[GADVideoOptions alloc ] init ];
152+ videoOptions.startMuted = YES ;
153+ signalRequest.videoOptions = videoOptions;
154+
155+ [GADMobileAds generateSignal: signalRequest
156+ completionHandler: ^(GADSignal *_Nullable signal, NSError *_Nullable error) {
157+ if (error != nil ) {
158+ NSLog (@" Error getting signal: %@ " , error.localizedDescription );
159+ return ;
160+ }
161+ if (signal == nil ) {
162+ NSLog (@" Unexpected error - signal is nil." );
163+ return ;
164+ }
165+ NSLog (@" Signal string: %@ " , signal.signalString );
166+ // Fetch the ad response using your generated signal.
167+ NSString *adResponseString = [self fetchAdResponseString: signal];
168+ [self renderNative: adUnitID
169+ adResponseString: adResponseString
170+ rootViewController: rootViewController];
171+ }];
172+ // [END native_ad_options]
173+ }
174+
175+ // [START fetch_response]
176+ // Emulates a request to your ad server.
177+ - (NSString *)fetchAdResponseString : (GADSignal *)signal {
178+ return @" adResponseString" ;
179+ }
180+ // [END fetch_response]
181+
182+ - (void )renderBanner : (NSString *)adUnitID
183+ adResponseString : (NSString *)adResponseString
184+ rootViewController : (UIViewController *)rootViewController {
185+ // [START render_banner]
186+ self.bannerView = [[GAMBannerView alloc ]
187+ initWithAdSize: GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 )];
188+ self.bannerView .adUnitID = adUnitID;
189+ self.bannerView .rootViewController = rootViewController;
190+ self.bannerView .delegate = self;
191+ [self .bannerView loadWithAdResponseString: adResponseString
192+ completionHandler: ^(NSError *_Nullable error) {
193+ if (error) {
194+ NSLog (@" Failed to load banner ad with error: %@ " , error);
195+ return ;
196+ }
197+ NSLog (@" Successfully loaded banner ad." );
198+ }];
199+ // [END render_banner]
200+ }
201+
202+ - (void )renderNative : (NSString *)adUnitID
203+ adResponseString : (NSString *)adResponseString
204+ rootViewController : (UIViewController *)rootViewController {
205+ // [START render_native]
206+ self.adLoader = [[GADAdLoader alloc ] initWithAdUnitID: adUnitID
207+ rootViewController: rootViewController
208+ adTypes: @[ GADAdLoaderAdTypeNative ]
209+ options: nil ];
210+ self.adLoader .delegate = self;
211+ [self .adLoader loadWithAdResponseString: adResponseString];
212+ // [END render_native]
213+ }
214+
215+ - (void )renderNativePlusBanner : (NSString *)adUnitID
216+ adResponseString : (NSString *)adResponseString
217+ rootViewController : (UIViewController *)rootViewController {
218+ // [START render_native_plus_banner]
219+ self.adLoader =
220+ [[GADAdLoader alloc ] initWithAdUnitID: adUnitID
221+ rootViewController: rootViewController
222+ adTypes: @[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]
223+ options: nil ];
224+ self.adLoader .delegate = self;
225+ [self .adLoader loadWithAdResponseString: adResponseString];
226+ // [END render_native_plus_banner]
227+ }
228+
229+ #pragma mark - GADBannerViewDelegate
230+
231+ - (void )bannerViewDidReceiveAd : (GADBannerView *)bannerView {
232+ NSLog (@" Banner ad received." );
233+ }
234+
235+ - (void )bannerView : (GADBannerView *)bannerView didFailToReceiveAdWithError : (NSError *)error {
236+ NSLog (@" Banner ad failed to load with error: %@ " , error);
237+ }
238+
239+ #pragma mark - GAMBannerAdLoaderDelegate
240+
241+ - (void )adLoader : (nonnull GADAdLoader *)adLoader
242+ didReceiveGAMBannerView : (nonnull GAMBannerView *)bannerView {
243+ // GAM Banner ad received.
244+ }
245+
246+ - (nonnull NSArray <NSValue *> *)validGAMBannerSizesForAdLoader : (nonnull GADAdLoader *)adLoader {
247+ return @[ NSValueFromGADAdSize(GADCurrentOrientationInlineAdaptiveBannerAdSizeWithWidth (375 )) ];
248+ }
249+
250+ #pragma mark - GADNativeAdLoaderDelegate
251+
252+ - (void )adLoader : (nonnull GADAdLoader *)adLoader
253+ didReceiveNativeAd : (nonnull GADNativeAd *)nativeAd {
254+ // Native ad received.
255+ }
256+
257+ - (void )adLoader : (nonnull GADAdLoader *)adLoader
258+ didFailToReceiveAdWithError : (nonnull NSError *)error {
259+ // Native ad failed to load.
260+ }
261+
112262@end
0 commit comments