From 98b0da86b82f2be18b4ae2a05996055deda6b117 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 | 49 +++++++++++++++++++ .../Native/NativeAdViewModel.swift | 2 +- .../Native/NativeContentView.swift | 2 +- .../AdDialogContentView.swift | 16 ++++++ .../RewardedInterstitialContentView.swift | 20 ++++++++ .../RewardedInterstitialViewModel.swift | 49 +++++++++++++++++++ .../Rewarded/RewardedContentView.swift | 21 +++++++- .../Rewarded/RewardedViewModel.swift | 49 +++++++++++++++++++ .../GoogleMobileAdsConsentManager.swift | 2 +- .../Supporting-Files/SwiftUIDemoApp.swift | 2 +- 11 files changed, 227 insertions(+), 5 deletions(-) diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift index 6c9f1161..647c1b8e 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialContentView.swift @@ -1,3 +1,19 @@ +// +// Copyright 2022 Google LLC +// +// 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] Hide from developer docs code snippet VStack(spacing: 20) { Text("The Impossible Game") .font(.largeTitle) @@ -32,6 +50,7 @@ struct InterstitialContentView: View { } .onDisappear { countdownTimer.pause() + // [END_EXCLUDE] } .onChange(of: countdownTimer.isComplete) { newValue in showGameOverAlert = newValue @@ -45,6 +64,7 @@ struct InterstitialContentView: View { action: { 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..71bf5d79 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialViewModel.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Interstitial/InterstitialViewModel.swift @@ -1,3 +1,20 @@ +// +// Copyright 2022 Google LLC +// +// 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_ad] import GoogleMobileAds class InterstitialViewModel: NSObject, GADFullScreenContentDelegate { @@ -7,12 +24,16 @@ class InterstitialViewModel: NSObject, GADFullScreenContentDelegate { do { interstitialAd = try await GADInterstitialAd.load( withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest()) + // [START set_the_delegate] interstitialAd?.fullScreenContentDelegate = self + // [END set_the_delegate] } catch { print("Failed to load interstitial ad with error: \(error.localizedDescription)") } } + // [END load_ad] + // [START show_ad] func showAd() { guard let interstitialAd = interstitialAd else { return print("Ad wasn't ready.") @@ -20,10 +41,38 @@ class InterstitialViewModel: NSObject, GADFullScreenContentDelegate { interstitialAd.present(fromRootViewController: nil) } + // [END show_ad] // MARK: - GADFullScreenContentDelegate methods + // [START ad_events] + 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] } diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeAdViewModel.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeAdViewModel.swift index 665313bb..ab8d08dd 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeAdViewModel.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeAdViewModel.swift @@ -1,5 +1,5 @@ // -// Copyright (C) 2022 Google, Inc. +// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeContentView.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeContentView.swift index 6df3a787..b4e4cafc 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeContentView.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Native/NativeContentView.swift @@ -1,5 +1,5 @@ // -// Copyright (C) 2022 Google, Inc. +// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/AdDialogContentView.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/AdDialogContentView.swift index cb13ecb8..e11f1e71 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/AdDialogContentView.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/AdDialogContentView.swift @@ -1,3 +1,19 @@ +// +// Copyright 2022 Google LLC +// +// 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 SwiftUI struct AdDialogContentView: View { diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialContentView.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialContentView.swift index 436a569c..e0d0f444 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialContentView.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialContentView.swift @@ -1,3 +1,19 @@ +// +// Copyright 2022 Google LLC +// +// 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 @@ -17,7 +33,9 @@ struct RewardedInterstitialContentView: View { } } + // [START show_ad] var rewardedInterstitialBody: some View { + // [START_EXCLUDE] Hide from developer docs code snippet VStack(spacing: 20) { Text("The Impossible Game") .font(.largeTitle) @@ -54,6 +72,7 @@ struct RewardedInterstitialContentView: View { showAdDialog = true viewModel.addCoins(1) } + // [END_EXCLUDE] } .onChange( of: showAd, @@ -63,6 +82,7 @@ struct RewardedInterstitialContentView: View { } } ) + // [END show_ad] .navigationTitle(navigationTitle) } diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialViewModel.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialViewModel.swift index 655e8ad3..6ac99732 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialViewModel.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded-Interstitial/RewardedInterstitialViewModel.swift @@ -1,3 +1,20 @@ +// +// Copyright 2022 Google LLC +// +// 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_ad] import GoogleMobileAds class RewardedInterstitialViewModel: NSObject, ObservableObject, @@ -10,13 +27,17 @@ class RewardedInterstitialViewModel: NSObject, ObservableObject, do { rewardedInterstitialAd = try await GADRewardedInterstitialAd.load( withAdUnitID: "ca-app-pub-3940256099942544/6978759866", request: GADRequest()) + // [START set_the_delegate] rewardedInterstitialAd?.fullScreenContentDelegate = self + // [END set_the_delegate] } catch { print( "Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") } } + // [END load_ad] + // [START show_ad] func showAd() { guard let rewardedInterstitialAd = rewardedInterstitialAd else { return print("Ad wasn't ready.") @@ -28,6 +49,7 @@ class RewardedInterstitialViewModel: NSObject, ObservableObject, self.addCoins(reward.amount.intValue) } } + // [END show_ad] func addCoins(_ amount: Int) { coins += amount @@ -35,7 +57,34 @@ class RewardedInterstitialViewModel: NSObject, ObservableObject, // MARK: - GADFullScreenContentDelegate methods + // [START ad_events] + 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 rewarded interstitial ad. rewardedInterstitialAd = nil } + // [END ad_events] } diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedContentView.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedContentView.swift index 7f27b663..9cf707f8 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedContentView.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedContentView.swift @@ -1,3 +1,19 @@ +// +// Copyright 2022 Google LLC +// +// 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,8 +23,10 @@ struct RewardedContentView: View { @State private var showWatchVideoButton = false let navigationTitle: String + // [START show_ad] var body: some View { VStack(spacing: 20) { + // [START_EXCLUDE silent] Hide from developer docs code snippet Text("The Impossible Game") .font(.largeTitle) @@ -21,11 +39,12 @@ struct RewardedContentView: View { Button("Play Again") { startNewGame() } - + // [END_EXCLUDE] Button("Watch video for additional 10 coins") { viewModel.showAd() showWatchVideoButton = false } + // [END show_ad] .opacity(showWatchVideoButton ? 1 : 0) } .font(.title2) diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedViewModel.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedViewModel.swift index 4e1c3aa3..a498b416 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedViewModel.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Rewarded/RewardedViewModel.swift @@ -1,3 +1,20 @@ +// +// Copyright 2022 Google LLC +// +// 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_ad] import GoogleMobileAds class RewardedViewModel: NSObject, ObservableObject, GADFullScreenContentDelegate { @@ -8,12 +25,16 @@ class RewardedViewModel: NSObject, ObservableObject, GADFullScreenContentDelegat do { rewardedAd = try await GADRewardedAd.load( withAdUnitID: "ca-app-pub-3940256099942544/1712485313", request: GADRequest()) + // [START set_the_delegate] rewardedAd?.fullScreenContentDelegate = self + // [END set_the_delegate] } catch { print("Failed to load rewarded ad with error: \(error.localizedDescription)") } } + // [END load_ad] + // [START show_ad] func showAd() { guard let rewardedAd = rewardedAd else { return print("Ad wasn't ready.") @@ -25,6 +46,7 @@ class RewardedViewModel: NSObject, ObservableObject, GADFullScreenContentDelegat self.addCoins(reward.amount.intValue) } } + // [END show_ad] func addCoins(_ amount: Int) { coins += amount @@ -32,7 +54,34 @@ class RewardedViewModel: NSObject, ObservableObject, GADFullScreenContentDelegat // MARK: - GADFullScreenContentDelegate methods + // [START ad_events] + 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 rewarded ad. rewardedAd = nil } + // [END ad_events] } diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/GoogleMobileAdsConsentManager.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/GoogleMobileAdsConsentManager.swift index 8bc3623c..b2adda9f 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/GoogleMobileAdsConsentManager.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/GoogleMobileAdsConsentManager.swift @@ -1,5 +1,5 @@ // -// Copyright (C) 2023 Google LLC +// Copyright 2023 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. diff --git a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/SwiftUIDemoApp.swift b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/SwiftUIDemoApp.swift index 94c83181..81843cd8 100644 --- a/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/SwiftUIDemoApp.swift +++ b/Swift/advanced/SwiftUIDemo/SwiftUIDemo/Supporting-Files/SwiftUIDemoApp.swift @@ -1,5 +1,5 @@ // -// Copyright (C) 2022 Google, Inc. +// Copyright 2022 Google LLC // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License.