From 1cff932b2a24484b01b3201db6ab042932e0a724 Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Tue, 6 Aug 2024 11:04:04 -0700 Subject: [PATCH] Add includecode tags for SwiftUI full-screen ad snippets PiperOrigin-RevId: 660022142 --- .../InterstitialContentView.swift | 20 ++++++++ .../Interstitial/InterstitialViewModel.swift | 50 ++++++++++++++++++- 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift index 6c9f1161..c8f4144e 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift @@ -1,3 +1,19 @@ +// +// Copyright (C) 2022 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 SwiftUI @@ -7,7 +23,9 @@ struct InterstitialContentView: View { private let viewModel = InterstitialViewModel() let navigationTitle: String + // [START show_ad] var body: some View { + // [START_EXCLUDE] VStack(spacing: 20) { Text("The Impossible Game") .font(.largeTitle) @@ -33,6 +51,7 @@ struct InterstitialContentView: View { .onDisappear { countdownTimer.pause() } + // [END_EXCLUDE] .onChange(of: countdownTimer.isComplete) { newValue in showGameOverAlert = newValue } @@ -46,6 +65,7 @@ struct InterstitialContentView: View { viewModel.showAd() })) } + // [END show_ad] .navigationTitle(navigationTitle) } diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialViewModel.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialViewModel.swift index 8d0e1d90..851cc7b2 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialViewModel.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialViewModel.swift @@ -1,8 +1,26 @@ +// +// Copyright (C) 2022 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. +// + +// [START load_interstitial_ad] import GoogleMobileAds class InterstitialViewModel: NSObject, GADFullScreenContentDelegate { private var interstitialAd: GADInterstitialAd? + // [START ad_events] func loadAd() async { do { interstitialAd = try await GADInterstitialAd.load( @@ -12,7 +30,10 @@ class InterstitialViewModel: NSObject, GADFullScreenContentDelegate { print("Failed to load interstitial ad with error: \(error.localizedDescription)") } } + // [END load_interstitial_ad] + // [START_EXCLUDE silent] + // [START show_ad] func showAd() { guard let interstitialAd = interstitialAd else { return print("Ad wasn't ready.") @@ -20,10 +41,37 @@ class InterstitialViewModel: NSObject, GADFullScreenContentDelegate { interstitialAd.present(fromRootViewController: nil) } - + // [END show_ad] + // [END_EXCLUDE silent] // MARK: - GADFullScreenContentDelegate methods + func adDidRecordImpression(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + + func adDidRecordClick(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + + func ad( + _ ad: GADFullScreenPresentingAd, + didFailToPresentFullScreenContentWithError error: Error + ) { + print("\(#function) called") + } + + func adWillPresentFullScreenContent(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + + func adWillDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + } + func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) { + print("\(#function) called") + // Clear the interstitial ad. interstitialAd = nil } + // [END ad_events] }