Skip to content

Commit

Permalink
Add includecode tags for SwiftUI full-screen ad snippets
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 660022142
  • Loading branch information
Justin Malandruccolo authored and maddevrelgithubbot committed Aug 6, 2024
1 parent 3c389ef commit 1cff932
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand All @@ -33,6 +51,7 @@ struct InterstitialContentView: View {
.onDisappear {
countdownTimer.pause()
}
// [END_EXCLUDE]
.onChange(of: countdownTimer.isComplete) { newValue in
showGameOverAlert = newValue
}
Expand All @@ -46,6 +65,7 @@ struct InterstitialContentView: View {
viewModel.showAd()
}))
}
// [END show_ad]
.navigationTitle(navigationTitle)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -12,18 +30,48 @@ 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.")
}

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]
}

0 comments on commit 1cff932

Please sign in to comment.