From 99648f28070b854c2249a4b0c0e75af98a5e4041 Mon Sep 17 00:00:00 2001 From: Nicholas Ventimiglia Date: Tue, 9 Sep 2025 15:55:21 -0700 Subject: [PATCH] Added snippets for server to server requests. PiperOrigin-RevId: 805096286 --- .../APIDemo/APIDemo.xcodeproj/project.pbxproj | 9 +- .../APIDemo/Snippets/AdManagerSCARSnippets.m | 110 ++++++++++++++++++ .../APIDemo/Snippets/AdmobSCARSnippets.m | 107 +++++++++++++++++ .../APIDemo/APIDemo.xcodeproj/project.pbxproj | 11 +- .../Snippets/AdManagerSCARSnippets.swift | 107 +++++++++++++++++ .../APIDemo/Snippets/AdmobSCARSnippets.swift | 104 +++++++++++++++++ 6 files changed, 444 insertions(+), 4 deletions(-) create mode 100644 Objective-C/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.m create mode 100644 Objective-C/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.m create mode 100644 Swift/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.swift create mode 100644 Swift/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.swift diff --git a/Objective-C/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj b/Objective-C/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj index 216b1af3..53d56670 100644 --- a/Objective-C/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj +++ b/Objective-C/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 1C1F6FCB2E70EA9300B6D6F5 /* AdManagerSCARSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = 1C1F6FC92E70EA9300B6D6F5 /* AdManagerSCARSnippets.m */; }; + 1C1F6FCC2E70EA9300B6D6F5 /* AdmobSCARSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = 1C1F6FCA2E70EA9300B6D6F5 /* AdmobSCARSnippets.m */; }; 1CD352992C9CE6A600534FCC /* CustomControls.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1CD352982C9CE6A600534FCC /* CustomControls.xib */; }; 4A96E7211B856D5000031470 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A96E7201B856D5000031470 /* main.m */; }; 4A96E7241B856D5000031470 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4A96E7231B856D5000031470 /* AppDelegate.m */; }; @@ -35,13 +37,14 @@ AED1111020320CA900EA4BEE /* NativeAdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = AED1110F20320CA800EA4BEE /* NativeAdView.xib */; }; AEE8DA9F201BD7A6008792E6 /* SimpleNativeAdView.m in Sources */ = {isa = PBXBuildFile; fileRef = AEE8DA9D201BD7A6008792E6 /* SimpleNativeAdView.m */; }; AEE8DAA5201BDCA9008792E6 /* CustomControlsView.m in Sources */ = {isa = PBXBuildFile; fileRef = AEE8DAA4201BDCA9008792E6 /* CustomControlsView.m */; }; - EF25D0222E4B916800688F43 /* BannerSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0212E4B916800688F43 /* BannerSnippets.m */; }; EF25D0242E4B917500688F43 /* ResponseInfoSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0232E4B917500688F43 /* ResponseInfoSnippets.m */; }; EF25D0262E4B95F400688F43 /* RewardedInterstitialAdSnippets.m in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0252E4B917E00688F43 /* RewardedInterstitialAdSnippets.m */; }; EF98F6062BD948C900FDCA41 /* CollapsibleBannerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = EF98F6052BD948C900FDCA41 /* CollapsibleBannerViewController.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 1C1F6FC92E70EA9300B6D6F5 /* AdManagerSCARSnippets.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AdManagerSCARSnippets.m; sourceTree = ""; }; + 1C1F6FCA2E70EA9300B6D6F5 /* AdmobSCARSnippets.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AdmobSCARSnippets.m; sourceTree = ""; }; 1CD352982C9CE6A600534FCC /* CustomControls.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CustomControls.xib; sourceTree = ""; }; 4A96E71B1B856D5000031470 /* APIDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; 4A96E71F1B856D5000031470 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; @@ -190,6 +193,8 @@ A8B6503B2DD63FAB00AE1D3F /* Snippets */ = { isa = PBXGroup; children = ( + 1C1F6FC92E70EA9300B6D6F5 /* AdManagerSCARSnippets.m */, + 1C1F6FCA2E70EA9300B6D6F5 /* AdmobSCARSnippets.m */, EF25D0252E4B917E00688F43 /* RewardedInterstitialAdSnippets.m */, EF25D0232E4B917500688F43 /* ResponseInfoSnippets.m */, EF25D0212E4B916800688F43 /* BannerSnippets.m */, @@ -314,6 +319,8 @@ A8265F742E4F8AE000A9C150 /* BannerSnippets.m in Sources */, A8265F752E4F8AE000A9C150 /* ResponseInfoSnippets.m in Sources */, 50DC0CFA212F1C4000424CC7 /* GAMFluidAdSizeViewController.m in Sources */, + 1C1F6FCB2E70EA9300B6D6F5 /* AdManagerSCARSnippets.m in Sources */, + 1C1F6FCC2E70EA9300B6D6F5 /* AdmobSCARSnippets.m in Sources */, A8B6503A2DD63FA500AE1D3F /* RequestConfigurationSnippets.m in Sources */, AEE8DAA5201BDCA9008792E6 /* CustomControlsView.m in Sources */, AEE8DA9F201BD7A6008792E6 /* SimpleNativeAdView.m in Sources */, diff --git a/Objective-C/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.m b/Objective-C/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.m new file mode 100644 index 00000000..6eef01bc --- /dev/null +++ b/Objective-C/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.m @@ -0,0 +1,110 @@ +// +// Copyright (C) 2025 Google, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import + +@interface AdManagerSCARSnippets : NSObject +- (void)loadNative:(NSString *)adUnitID; +- (void)loadBanner:(NSString *)adUnitID; +- (void)loadNativePlusBanner:(NSString *)adUnitID; +@end + +@implementation AdManagerSCARSnippets + +- (void)loadNative:(NSString *)adUnitID { + // [START signal_request_native] + // Create a signal request for an ad. + // Specify the "signal_type_ad_manager_s2s" to + // denote that the usage of QueryInfo is for Ad Manager S2S. + GADNativeSignalRequest *signalRequest = + [[GADNativeSignalRequest alloc] initWithSignalType:@"signal_type_ad_manager_s2s"]; + signalRequest.requestAgent = @"request_agent"; + signalRequest.adUnitID = adUnitID; + + [GADMobileAds generateSignal:signalRequest + completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) { + if (error != nil) { + NSLog(@"Error getting ad info: %@", error.localizedDescription); + return; + } + if (signal == nil) { + NSLog(@"Unexpected error - query info is nil."); + return; + } + NSLog(@"Signal string: %@", signal.signalString); + // TODO: Fetch the ad response using your generated signal. + }]; + // [END signal_request_native] +} + +- (void)loadBanner:(NSString *)adUnitID { + // [START signal_request_banner] + // Create a signal request for an ad. + // Specify the "signal_type_ad_manager_s2s" to + // denote that the usage of QueryInfo is for Ad Manager S2S. + GADBannerSignalRequest *signalRequest = + [[GADBannerSignalRequest alloc] initWithSignalType:@"signal_type_ad_manager_s2s"]; + signalRequest.requestAgent = @"request_agent"; + signalRequest.adUnitID = adUnitID; + signalRequest.adSize = GADPortraitInlineAdaptiveBannerAdSizeWithWidth(320); + + [GADMobileAds generateSignal:signalRequest + completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) { + if (error != nil) { + NSLog(@"Error getting ad info: %@", error.localizedDescription); + return; + } + if (signal == nil) { + NSLog(@"Unexpected error - query info is nil."); + return; + } + NSLog(@"Signal string: %@", signal.signalString); + // TODO: Fetch the ad response using your generated signal. + }]; + // [END signal_request_banner] +} + +- (void)loadNativePlusBanner:(NSString *)adUnitID { + // [START signal_request_native_plus_banner] + // Create a signal request for an ad. + // Specify the "signal_type_ad_manager_s2s" to + // denote that the usage of QueryInfo is for Ad Manager S2S. + GADNativeSignalRequest *signalRequest = + [[GADNativeSignalRequest alloc] initWithSignalType:@"signal_type_ad_manager_s2s"]; + signalRequest.requestAgent = @"request_agent"; + signalRequest.adUnitID = adUnitID; + signalRequest.adLoaderAdTypes = + [NSSet setWithArray:@[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]]; + signalRequest.adSizes = @[ @(GADPortraitInlineAdaptiveBannerAdSizeWithWidth(320)) ]; + + [GADMobileAds generateSignal:signalRequest + completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) { + if (error != nil) { + NSLog(@"Error getting ad info: %@", error.localizedDescription); + return; + } + if (signal == nil) { + NSLog(@"Unexpected error - query info is nil."); + return; + } + NSLog(@"Signal string: %@", signal.signalString); + // TODO: Fetch the ad response using your generated signal. + }]; + // [END signal_request_native_plus_banner] +} + +@end diff --git a/Objective-C/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.m b/Objective-C/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.m new file mode 100644 index 00000000..6ac285e0 --- /dev/null +++ b/Objective-C/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.m @@ -0,0 +1,107 @@ +// +// Copyright (C) 2025 Google, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +#import +#import + +@interface AdmobSCARSnippets : NSObject +- (void)loadNative:(NSString *)adUnitID; +- (void)loadBanner:(NSString *)adUnitID; +- (void)loadNativePlusBanner:(NSString *)adUnitID; +@end + +@implementation AdmobSCARSnippets + +- (void)loadNative:(NSString *)adUnitID { + // [START signal_request_native] + // Create a signal request for an ad. + // Replace REQUEST_TYPE with your request type. + GADNativeSignalRequest *signalRequest = + [[GADNativeSignalRequest alloc] initWithSignalType:@"REQUEST_TYPE"]; + signalRequest.requestAgent = @"request_agent"; + signalRequest.adUnitID = adUnitID; + + [GADMobileAds generateSignal:signalRequest + completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) { + if (error != nil) { + NSLog(@"Error getting ad info: %@", error.localizedDescription); + return; + } + if (signal == nil) { + NSLog(@"Unexpected error - query info is nil."); + return; + } + NSLog(@"Signal string: %@", signal.signalString); + // TODO: Fetch the ad response using your generated signal. + }]; + // [END signal_request_native] +} + +- (void)loadBanner:(NSString *)adUnitID { + // [START signal_request_banner] + // Create a signal request for an ad. + // Replace REQUEST_TYPE with your request type. + GADBannerSignalRequest *signalRequest = + [[GADBannerSignalRequest alloc] initWithSignalType:@"REQUEST_TYPE"]; + signalRequest.requestAgent = @"request_agent"; + signalRequest.adUnitID = adUnitID; + signalRequest.adSize = GADPortraitInlineAdaptiveBannerAdSizeWithWidth(320); + + [GADMobileAds generateSignal:signalRequest + completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) { + if (error != nil) { + NSLog(@"Error getting ad info: %@", error.localizedDescription); + return; + } + if (signal == nil) { + NSLog(@"Unexpected error - query info is nil."); + return; + } + NSLog(@"Signal string: %@", signal.signalString); + // TODO: Fetch the ad response using your generated signal. + }]; + // [END signal_request_banner] +} + +- (void)loadNativePlusBanner:(NSString *)adUnitID { + // [START signal_request_native_plus_banner] + // Create a signal request for an ad. + // Replace REQUEST_TYPE with your request type. + GADNativeSignalRequest *signalRequest = + [[GADNativeSignalRequest alloc] initWithSignalType:@"REQUEST_TYPE"]; + signalRequest.requestAgent = @"request_agent"; + signalRequest.adUnitID = adUnitID; + signalRequest.adLoaderAdTypes = + [NSSet setWithArray:@[ GADAdLoaderAdTypeNative, GADAdLoaderAdTypeGAMBanner ]]; + signalRequest.adSizes = @[ @(GADPortraitInlineAdaptiveBannerAdSizeWithWidth(320)) ]; + + [GADMobileAds generateSignal:signalRequest + completionHandler:^(GADSignal *_Nullable signal, NSError *_Nullable error) { + if (error != nil) { + NSLog(@"Error getting ad info: %@", error.localizedDescription); + return; + } + if (signal == nil) { + NSLog(@"Unexpected error - query info is nil."); + return; + } + NSLog(@"Signal string: %@", signal.signalString); + // TODO: Fetch the ad response using your generated signal. + }]; + // [END signal_request_native_plus_banner] +} + +@end diff --git a/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj b/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj index b731f71d..87c3e4c0 100644 --- a/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj +++ b/Swift/advanced/APIDemo/APIDemo.xcodeproj/project.pbxproj @@ -7,6 +7,8 @@ objects = { /* Begin PBXBuildFile section */ + 1C1F6FC72E70E87700B6D6F5 /* AdManagerSCARSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C1F6FC52E70E87700B6D6F5 /* AdManagerSCARSnippets.swift */; }; + 1C1F6FC82E70E87700B6D6F5 /* AdmobSCARSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1C1F6FC62E70E87700B6D6F5 /* AdmobSCARSnippets.swift */; }; 1CD352682C9CC97200534FCC /* CustomControls.xib in Resources */ = {isa = PBXBuildFile; fileRef = 1CD352672C9CC97200534FCC /* CustomControls.xib */; }; 4A7A6CD71C76237500FB1A32 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4A7A6CD61C76237500FB1A32 /* Constants.swift */; }; 4AA7D6911C625A1200DFD2EB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4AA7D6901C625A1200DFD2EB /* AppDelegate.swift */; }; @@ -24,7 +26,6 @@ 507818F8219A418100E5A44A /* AdManagerCustomVideoControlsController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 507818F1219A417F00E5A44A /* AdManagerCustomVideoControlsController.swift */; }; 507818F9219A418100E5A44A /* AdManagerAppEventsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 507818F2219A418000E5A44A /* AdManagerAppEventsViewController.swift */; }; 507818FA219A418100E5A44A /* AdManagerFluidAdSizeViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 507818F3219A418100E5A44A /* AdManagerFluidAdSizeViewController.swift */; }; - A8265F6D2E4F882600A9C150 /* BannerSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8265F6C2E4F882600A9C150 /* BannerSnippets.swift */; }; A8265F6F2E4F883500A9C150 /* ResponseInfoSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8265F6E2E4F883500A9C150 /* ResponseInfoSnippets.swift */; }; A8265F712E4F884500A9C150 /* NativeAdOptionsSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8265F702E4F884100A9C150 /* NativeAdOptionsSnippets.swift */; }; A846F1C12E2583D200F96E49 /* RewardedAdSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = A846F1C02E2583CF00F96E49 /* RewardedAdSnippets.swift */; }; @@ -35,14 +36,14 @@ AED11120203213FC00EA4BEE /* UnifiedNativeAdView.xib in Resources */ = {isa = PBXBuildFile; fileRef = AED11118203213F500EA4BEE /* UnifiedNativeAdView.xib */; }; AED111262032151200EA4BEE /* SimpleNativeAdView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AED111252032151200EA4BEE /* SimpleNativeAdView.swift */; }; AED111282032174100EA4BEE /* CustomControlsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = AED111272032174100EA4BEE /* CustomControlsView.swift */; }; - EF25D0282E4B97F000688F43 /* BannerSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0272E4B97F000688F43 /* BannerSnippets.swift */; }; - EF25D02A2E4B97FA00688F43 /* ResponseInfoSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF25D0292E4B97FA00688F43 /* ResponseInfoSnippets.swift */; }; EF25D02C2E4B980C00688F43 /* RewardedInterstitialAdSnippets.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF25D02B2E4B980100688F43 /* RewardedInterstitialAdSnippets.swift */; }; EFC712462AD853D80014A2BF /* CollapsibleBannerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EFC712452AD853D80014A2BF /* CollapsibleBannerViewController.swift */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ 15CB61211C7D0256000212DE /* APIDemo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "APIDemo-Bridging-Header.h"; sourceTree = ""; }; + 1C1F6FC52E70E87700B6D6F5 /* AdManagerSCARSnippets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdManagerSCARSnippets.swift; sourceTree = ""; }; + 1C1F6FC62E70E87700B6D6F5 /* AdmobSCARSnippets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AdmobSCARSnippets.swift; sourceTree = ""; }; 1CD352672C9CC97200534FCC /* CustomControls.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CustomControls.xib; sourceTree = ""; }; 4A7A6CD61C76237500FB1A32 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; 4AA7D68D1C625A1200DFD2EB /* APIDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = APIDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -146,6 +147,8 @@ A8B650372DD63E0F00AE1D3F /* Snippets */ = { isa = PBXGroup; children = ( + 1C1F6FC52E70E87700B6D6F5 /* AdManagerSCARSnippets.swift */, + 1C1F6FC62E70E87700B6D6F5 /* AdmobSCARSnippets.swift */, EF25D02B2E4B980100688F43 /* RewardedInterstitialAdSnippets.swift */, EF25D0292E4B97FA00688F43 /* ResponseInfoSnippets.swift */, EF25D0272E4B97F000688F43 /* BannerSnippets.swift */, @@ -256,6 +259,8 @@ buildActionMask = 2147483647; files = ( 4AA7D6AA1C626E7000DFD2EB /* AdMobAdTargetingTableViewController.swift in Sources */, + 1C1F6FC72E70E87700B6D6F5 /* AdManagerSCARSnippets.swift in Sources */, + 1C1F6FC82E70E87700B6D6F5 /* AdmobSCARSnippets.swift in Sources */, 507818F5219A418100E5A44A /* AdManagerCategoryExclusionsTableViewController.swift in Sources */, 4AA7D6911C625A1200DFD2EB /* AppDelegate.swift in Sources */, 4AA7D6AC1C626EE700DFD2EB /* AdMobAdDelegateViewController.swift in Sources */, diff --git a/Swift/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.swift b/Swift/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.swift new file mode 100644 index 00000000..1b3a6059 --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/Snippets/AdManagerSCARSnippets.swift @@ -0,0 +1,107 @@ +// Copyright (C) 2025 Google, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import GoogleMobileAds +import UIKit + +class AdManagerSCARSnippets: NSObject { + + func loadNative(adUnitID: String) { + // [START signal_request_native] + // Create a signal request for an ad. + // Specify the "signal_type_ad_manager_s2s" to + // denote that the usage of QueryInfo is for Ad Manager S2S. + let signalRequest = NativeSignalRequest(signalType: "signal_type_ad_manager_s2s") + signalRequest.requestAgent = "request_agent" + signalRequest.adUnitID = adUnitID + + MobileAds.generateSignal(signalRequest) { [weak self] signal, error in + guard self != nil else { return } + + if let error = error { + print("Error getting ad info: \(error.localizedDescription)") + return + } + + guard let signal = signal else { + print("Unexpected error - signal info is nil.") + return + } + + print("Signal string: \(signal.signal)") + // TODO: Fetch the ad response using your generated signal. + } + // [END signal_request_native] + } + + func loadBanner(adUnitID: String) { + // [START signal_request_banner] + // Create a signal request for an ad. + // Specify the "signal_type_ad_manager_s2s" to + // denote that the usage of QueryInfo is for Ad Manager S2S. + let signalRequest = BannerSignalRequest(signalType: "signal_type_ad_manager_s2s") + signalRequest.requestAgent = "request_agent" + signalRequest.adUnitID = adUnitID + signalRequest.adSize = portraitInlineAdaptiveBanner(width: 320) + + MobileAds.generateSignal(signalRequest) { [weak self] signal, error in + guard self != nil else { return } + + if let error = error { + print("Error getting ad info: \(error.localizedDescription)") + return + } + + guard let signal = signal else { + print("Unexpected error - signal info is nil.") + return + } + + print("Signal string: \(signal.signal)") + // TODO: Fetch the ad response using your generated signal. + } + // [END signal_request_banner] + } + + func loadNativePlusBanner(adUnitID: String) { + // [START signal_request_native_plus_banner] + // Create a signal request for an ad. + // Specify the "signal_type_ad_manager_s2s" to + // denote that the usage of QueryInfo is for Ad Manager S2S. + let signalRequest = NativeSignalRequest(signalType: "signal_type_ad_manager_s2s") + signalRequest.requestAgent = "request_agent" + signalRequest.adUnitID = adUnitID + signalRequest.adLoaderAdTypes = [AdLoaderAdType.native, AdLoaderAdType.adManagerBanner] + signalRequest.adSizes = [nsValue(for: portraitInlineAdaptiveBanner(width: 320))] + + MobileAds.generateSignal(signalRequest) { [weak self] signal, error in + guard self != nil else { return } + + if let error = error { + print("Error getting ad info: \(error.localizedDescription)") + return + } + + guard let signal = signal else { + print("Unexpected error - signal info is nil.") + return + } + + print("Signal string: \(signal.signal)") + // TODO: Fetch the ad response using your generated signal. + } + // [END signal_request_native_plus_banner] + } +} diff --git a/Swift/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.swift b/Swift/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.swift new file mode 100644 index 00000000..46f25c2f --- /dev/null +++ b/Swift/advanced/APIDemo/APIDemo/Snippets/AdmobSCARSnippets.swift @@ -0,0 +1,104 @@ +// Copyright (C) 2025 Google, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import GoogleMobileAds +import UIKit + +class AdmobSCARSnippets: NSObject { + + func loadNative(adUnitID: String) { + // [START signal_request_native] + // Create a signal request for an ad. + // Replace REQUEST_TYPE with your request type. + let signalRequest = NativeSignalRequest(signalType: "REQUEST_TYPE") + signalRequest.requestAgent = "request_agent" + signalRequest.adUnitID = adUnitID + + MobileAds.generateSignal(signalRequest) { [weak self] signal, error in + guard self != nil else { return } + + if let error = error { + print("Error getting ad info: \(error.localizedDescription)") + return + } + + guard let signal = signal else { + print("Unexpected error - signal info is nil.") + return + } + + print("Signal string: \(signal.signal)") + // TODO: Fetch the ad response using your generated signal. + } + // [END signal_request_native] + } + + func loadBanner(adUnitID: String) { + // [START signal_request_banner] + // Create a signal request for an ad. + // Replace REQUEST_TYPE with your request type. + let signalRequest = BannerSignalRequest(signalType: "REQUEST_TYPE") + signalRequest.requestAgent = "request_agent" + signalRequest.adUnitID = adUnitID + signalRequest.adSize = portraitInlineAdaptiveBanner(width: 320) + + MobileAds.generateSignal(signalRequest) { [weak self] signal, error in + guard self != nil else { return } + + if let error = error { + print("Error getting ad info: \(error.localizedDescription)") + return + } + + guard let signal = signal else { + print("Unexpected error - signal info is nil.") + return + } + + print("Signal string: \(signal.signal)") + // TODO: Fetch the ad response using your generated signal. + } + // [END signal_request_banner] + } + + func loadNativePlusBanner(adUnitID: String) { + // [START signal_request_native_plus_banner] + // Create a signal request for an ad. + // Replace REQUEST_TYPE with your request type. + let signalRequest = NativeSignalRequest(signalType: "REQUEST_TYPE") + signalRequest.requestAgent = "request_agent" + signalRequest.adUnitID = adUnitID + signalRequest.adLoaderAdTypes = [AdLoaderAdType.native, AdLoaderAdType.adManagerBanner] + signalRequest.adSizes = [nsValue(for: portraitInlineAdaptiveBanner(width: 320))] + + MobileAds.generateSignal(signalRequest) { [weak self] signal, error in + guard self != nil else { return } + + if let error = error { + print("Error getting ad info: \(error.localizedDescription)") + return + } + + guard let signal = signal else { + print("Unexpected error - signal info is nil.") + return + } + + print("Signal string: \(signal.signal)") + // TODO: Fetch the ad response using your generated signal. + } + // [END signal_request_native_plus_banner] + } +}