Skip to content

Commit

Permalink
Refactored Swift samples to implement Swift concurrency for Ad loading
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 610522817
  • Loading branch information
Justin Malandruccolo authored and maddevrelgithubbot committed Feb 26, 2024
1 parent e07ed14 commit c4d10a7
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,26 @@ class AppOpenAdManager: NSObject {
appOpenAdManagerDelegate?.appOpenAdManagerAdDidComplete(self)
}

func loadAd() {
func loadAd() async {
// Do not load ad if there is an unused ad or one is already loading.
if isLoadingAd || isAdAvailable() {
return
}
isLoadingAd = true

print("Start loading app open ad.")
GADAppOpenAd.load(
withAdUnitID: "/6499/example/app-open",
request: GADRequest()
) { ad, error in
self.isLoadingAd = false
if let error = error {
self.appOpenAd = nil
self.loadTime = nil
print("App open ad failed to load with error: \(error.localizedDescription)")
return
}

self.appOpenAd = ad
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
print("App open ad loaded successfully.")
do {
appOpenAd = try await GADAppOpenAd.load(
withAdUnitID: "/6499/example/app-open", request: GAMRequest())
appOpenAd?.fullScreenContentDelegate = self
loadTime = Date()
isLoadingAd = false
} catch {
appOpenAd = nil
loadTime = nil
print("App open ad failed to load with error: \(error.localizedDescription)")
isLoadingAd = false
}
}

Expand All @@ -98,7 +95,9 @@ class AppOpenAdManager: NSObject {
print("App open ad is not ready yet.")
appOpenAdManagerAdDidComplete()
if GoogleMobileAdsConsentManager.shared.canRequestAds {
loadAd()
Task {
await loadAd()
}
}
return
}
Expand All @@ -120,7 +119,9 @@ extension AppOpenAdManager: GADFullScreenContentDelegate {
isShowingAd = false
print("App open ad was dismissed.")
appOpenAdManagerAdDidComplete()
loadAd()
Task {
await loadAd()
}
}

func ad(
Expand All @@ -131,6 +132,8 @@ extension AppOpenAdManager: GADFullScreenContentDelegate {
isShowingAd = false
print("App open ad failed to present with error: \(error.localizedDescription)")
appOpenAdManagerAdDidComplete()
loadAd()
Task {
await loadAd()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class SplashViewController: UIViewController, AppOpenAdManagerDelegate {
GADMobileAds.sharedInstance().start()

// Load an ad.
AppOpenAdManager.shared.loadAd()
Task {
await AppOpenAdManager.shared.loadAd()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start()
// Request an ad.
self.loadInterstitial()
Task {
await self.loadInterstitial()
}
}
}

Expand All @@ -168,17 +170,13 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
repeats: true)
}

fileprivate func loadInterstitial() {
GAMInterstitialAd.load(
withAdManagerAdUnitID: "/6499/example/interstitial",
request: GAMRequest()
) { (ad, error) in
if let error = error {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
return
}
self.interstitial = ad
self.interstitial?.fullScreenContentDelegate = self
fileprivate func loadInterstitial() async {
do {
interstitial = try await GAMInterstitialAd.load(
withAdManagerAdUnitID: "/6499/example/interstitial", request: GAMRequest())
interstitial?.fullScreenContentDelegate = self
} catch {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
}
}

Expand Down Expand Up @@ -251,7 +249,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
startNewGame()

if GoogleMobileAdsConsentManager.shared.canRequestAds {
loadInterstitial()
Task {
await loadInterstitial()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ class GameViewController: UIViewController, GADFullScreenContentDelegate {
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start()
// Request an ad.
self.loadRewardedInterstitialAd()
Task {
await self.loadRewardedInterstitialAd()
}
}
}

Expand All @@ -129,18 +131,14 @@ class GameViewController: UIViewController, GADFullScreenContentDelegate {
repeats: true)
}

private func loadRewardedInterstitialAd() {
let request = GAMRequest()
GADRewardedInterstitialAd.load(
withAdUnitID: "/21775744923/example/rewarded_interstitial", request: request
) { (ad, error) in
if let error = error {
print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
self.playAgainButton.isHidden = false
return
}
self.rewardedInterstitialAd = ad
self.rewardedInterstitialAd?.fullScreenContentDelegate = self
private func loadRewardedInterstitialAd() async {
do {
rewardedInterstitialAd = try await GADRewardedInterstitialAd.load(
withAdUnitID: "/21775744923/example/rewarded_interstitial", request: GAMRequest())
rewardedInterstitialAd?.fullScreenContentDelegate = self
} catch {
print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)")
playAgainButton.isHidden = false
}
}

Expand Down Expand Up @@ -260,7 +258,9 @@ class GameViewController: UIViewController, GADFullScreenContentDelegate {
@IBAction func playAgain(_ sender: AnyObject) {
startNewGame()
if GoogleMobileAdsConsentManager.shared.canRequestAds {
loadRewardedInterstitialAd()
Task {
await loadRewardedInterstitialAd()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22113.1" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="pGH-Ev-CeE">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="22505" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" colorMatched="YES" initialViewController="pGH-Ev-CeE">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment version="2048" identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22089"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="22504"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -101,6 +101,7 @@
<outlet property="coinCountLabel" destination="QZR-go-ZKi" id="phD-yy-NM6"/>
<outlet property="gameText" destination="wpQ-a9-WRd" id="jgH-bl-10i"/>
<outlet property="playAgainButton" destination="NoO-Ga-hur" id="NmL-nh-1EK"/>
<outlet property="privacySettingsButton" destination="xfn-We-cUj" id="xiD-lz-y0b"/>
<outlet property="watchVideoButton" destination="uTO-QG-Mlb" id="mbV-qa-rVp"/>
</connections>
</viewController>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,20 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start()
// Request an ad.
self.loadRewardedAd()
Task {
await self.loadRewardedAd()
}

}
}

func loadRewardedAd() {
GADRewardedAd.load(
withAdUnitID: "/6499/example/rewarded-video", request: GAMRequest()
) { (ad, error) in
if let error = error {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
return
}
print("Loading Succeeded")
self.rewardedAd = ad
self.rewardedAd?.fullScreenContentDelegate = self
func loadRewardedAd() async {
do {
rewardedAd = try await GADRewardedAd.load(
withAdUnitID: "/6499/example/rewarded-video", request: GAMRequest())
rewardedAd?.fullScreenContentDelegate = self
} catch {
print("Rewarded ad failed to load with error: \(error.localizedDescription)")
}
}

Expand Down Expand Up @@ -257,7 +256,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
@IBAction func playAgain(_ sender: AnyObject) {
startNewGame()
if GoogleMobileAdsConsentManager.shared.canRequestAds {
loadRewardedAd()
Task {
await loadRewardedAd()
}
}
}

Expand Down
41 changes: 22 additions & 19 deletions Swift/admob/AppOpenExample/AppOpenExample/AppOpenAdManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,26 @@ class AppOpenAdManager: NSObject {
appOpenAdManagerDelegate?.appOpenAdManagerAdDidComplete(self)
}

func loadAd() {
func loadAd() async {
// Do not load ad if there is an unused ad or one is already loading.
if isLoadingAd || isAdAvailable() {
return
}
isLoadingAd = true

print("Start loading app open ad.")
GADAppOpenAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/5575463023",
request: GADRequest()
) { ad, error in
self.isLoadingAd = false
if let error = error {
self.appOpenAd = nil
self.loadTime = nil
print("App open ad failed to load with error: \(error.localizedDescription)")
return
}

self.appOpenAd = ad
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
print("App open ad loaded successfully.")
do {
appOpenAd = try await GADAppOpenAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/5575463023", request: GADRequest())
appOpenAd?.fullScreenContentDelegate = self
loadTime = Date()
isLoadingAd = false
} catch {
appOpenAd = nil
loadTime = nil
print("App open ad failed to load with error: \(error.localizedDescription)")
isLoadingAd = false
}
}

Expand All @@ -98,7 +95,9 @@ class AppOpenAdManager: NSObject {
print("App open ad is not ready yet.")
appOpenAdManagerAdDidComplete()
if GoogleMobileAdsConsentManager.shared.canRequestAds {
loadAd()
Task {
await loadAd()
}
}
return
}
Expand All @@ -120,7 +119,9 @@ extension AppOpenAdManager: GADFullScreenContentDelegate {
isShowingAd = false
print("App open ad was dismissed.")
appOpenAdManagerAdDidComplete()
loadAd()
Task {
await loadAd()
}
}

func ad(
Expand All @@ -131,6 +132,8 @@ extension AppOpenAdManager: GADFullScreenContentDelegate {
isShowingAd = false
print("App open ad failed to present with error: \(error.localizedDescription)")
appOpenAdManagerAdDidComplete()
loadAd()
Task {
await loadAd()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ class SplashViewController: UIViewController, AppOpenAdManagerDelegate {
GADMobileAds.sharedInstance().start()

// Load an ad.
AppOpenAdManager.shared.loadAd()
Task {
await AppOpenAdManager.shared.loadAd()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
// Initialize the Google Mobile Ads SDK.
GADMobileAds.sharedInstance().start()
// Request an ad.
self.loadInterstitial()
Task {
await self.loadInterstitial()
}
}
}

Expand All @@ -169,17 +171,13 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
repeats: true)
}

fileprivate func loadInterstitial() {
let request = GADRequest()
GADInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: request
) { (ad, error) in
if let error = error {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
return
}
self.interstitial = ad
self.interstitial?.fullScreenContentDelegate = self
fileprivate func loadInterstitial() async {
do {
interstitial = try await GADInterstitialAd.load(
withAdUnitID: "ca-app-pub-3940256099942544/4411468910", request: GADRequest())
interstitial?.fullScreenContentDelegate = self
} catch {
print("Failed to load interstitial ad with error: \(error.localizedDescription)")
}
}

Expand Down Expand Up @@ -252,7 +250,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate {
startNewGame()

if GoogleMobileAdsConsentManager.shared.canRequestAds {
loadInterstitial()
Task {
await loadInterstitial()
}
}
}

Expand Down
Loading

0 comments on commit c4d10a7

Please sign in to comment.