|
| 1 | +// Copyright (C) 2025 Google, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | + |
| 16 | +import GoogleMobileAds |
| 17 | +import UIKit |
| 18 | + |
| 19 | +class AdManagerSCARSnippets: NSObject { |
| 20 | + |
| 21 | + func loadNative(adUnitID: String) async { |
| 22 | + // [START signal_request_native] |
| 23 | + // Create a signal request for an ad. |
| 24 | + // Specify the "signal_type_ad_manager_s2s" to |
| 25 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 26 | + let signalRequest = NativeSignalRequest(signalType: "signal_type_ad_manager_s2s") |
| 27 | + signalRequest.requestAgent = "REQUEST_AGENT" |
| 28 | + signalRequest.adUnitID = adUnitID |
| 29 | + |
| 30 | + do { |
| 31 | + let signal = try await MobileAds.generateSignal(signalRequest) |
| 32 | + print("Signal string: \(signal.signal)") |
| 33 | + // TODO: Fetch the ad response using your generated signal. |
| 34 | + } catch { |
| 35 | + print("Error getting ad info: \(error.localizedDescription)") |
| 36 | + } |
| 37 | + // [END signal_request_native] |
| 38 | + } |
| 39 | + |
| 40 | + func loadBanner(adUnitID: String) async { |
| 41 | + // [START signal_request_banner] |
| 42 | + // Create a signal request for an ad. |
| 43 | + // Specify the "signal_type_ad_manager_s2s" to |
| 44 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 45 | + let signalRequest = BannerSignalRequest(signalType: "signal_type_ad_manager_s2s") |
| 46 | + signalRequest.requestAgent = "REQUEST_AGENT" |
| 47 | + signalRequest.adUnitID = adUnitID |
| 48 | + // Refer to the AdSize class for available ad sizes. |
| 49 | + signalRequest.adSize = currentOrientationInlineAdaptiveBanner(width: 320) |
| 50 | + |
| 51 | + do { |
| 52 | + let signal = try await MobileAds.generateSignal(signalRequest) |
| 53 | + print("Signal string: \(signal.signal)") |
| 54 | + // TODO: Fetch the ad response using your generated signal. |
| 55 | + } catch { |
| 56 | + print("Error getting ad info: \(error.localizedDescription)") |
| 57 | + } |
| 58 | + // [END signal_request_banner] |
| 59 | + } |
| 60 | + |
| 61 | + func loadNativePlusBanner(adUnitID: String) async { |
| 62 | + // [START signal_request_native_plus_banner] |
| 63 | + // Create a signal request for an ad. |
| 64 | + // Specify the "signal_type_ad_manager_s2s" to |
| 65 | + // denote that the usage of QueryInfo is for Ad Manager S2S. |
| 66 | + let signalRequest = NativeSignalRequest(signalType: "signal_type_ad_manager_s2s") |
| 67 | + signalRequest.requestAgent = "REQUEST_AGENT" |
| 68 | + signalRequest.adUnitID = adUnitID |
| 69 | + signalRequest.adLoaderAdTypes = [AdLoaderAdType.native, AdLoaderAdType.adManagerBanner] |
| 70 | + // Refer to the AdSize class for available ad sizes. |
| 71 | + signalRequest.adSizes = [nsValue(for: currentOrientationInlineAdaptiveBanner(width: 320))] |
| 72 | + |
| 73 | + do { |
| 74 | + let signal = try await MobileAds.generateSignal(signalRequest) |
| 75 | + print("Signal string: \(signal.signal)") |
| 76 | + // TODO: Fetch the ad response using your generated signal. |
| 77 | + } catch { |
| 78 | + print("Error getting ad info: \(error.localizedDescription)") |
| 79 | + } |
| 80 | + // [END signal_request_native_plus_banner] |
| 81 | + } |
| 82 | +} |
0 commit comments