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 copybara-github committed Aug 20, 2024
1 parent b01b1e0 commit 98b0da8
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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

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] Hide from developer docs code snippet
VStack(spacing: 20) {
Text("The Impossible Game")
.font(.largeTitle)
Expand All @@ -32,6 +50,7 @@ struct InterstitialContentView: View {
}
.onDisappear {
countdownTimer.pause()
// [END_EXCLUDE]
}
.onChange(of: countdownTimer.isComplete) { newValue in
showGameOverAlert = newValue
Expand All @@ -45,6 +64,7 @@ struct InterstitialContentView: View {
action: {
viewModel.showAd()
}))
// [END show_ad]
}
.navigationTitle(navigationTitle)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -7,23 +24,55 @@ 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.")
}

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]
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -54,6 +72,7 @@ struct RewardedInterstitialContentView: View {
showAdDialog = true
viewModel.addCoins(1)
}
// [END_EXCLUDE]
}
.onChange(
of: showAd,
Expand All @@ -63,6 +82,7 @@ struct RewardedInterstitialContentView: View {
}
}
)
// [END show_ad]
.navigationTitle(navigationTitle)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.")
Expand All @@ -28,14 +49,42 @@ class RewardedInterstitialViewModel: NSObject, ObservableObject,
self.addCoins(reward.amount.intValue)
}
}
// [END show_ad]

func addCoins(_ amount: Int) {
coins += amount
}

// 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]
}
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)

Expand All @@ -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)
Expand Down
Loading

0 comments on commit 98b0da8

Please sign in to comment.