From c4d10a7209747bdc54f96dfd17674dfdab40c81c Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Mon, 26 Feb 2024 14:11:32 -0800 Subject: [PATCH] Refactored Swift samples to implement Swift concurrency for Ad loading PiperOrigin-RevId: 610522817 --- .../AppOpenAdManager.swift | 41 ++++++++++--------- .../SplashViewController.swift | 4 +- .../ViewController.swift | 26 ++++++------ .../GameViewController.swift | 28 ++++++------- .../Base.lproj/Main.storyboard | 5 ++- .../ViewController.swift | 27 ++++++------ .../AppOpenExample/AppOpenAdManager.swift | 41 ++++++++++--------- .../AppOpenExample/SplashViewController.swift | 4 +- .../InterstitialExample/ViewController.swift | 26 ++++++------ .../GameViewController.swift | 28 ++++++------- .../RewardedVideoExample/ViewController.swift | 26 ++++++------ 11 files changed, 134 insertions(+), 122 deletions(-) diff --git a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/AppOpenAdManager.swift b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/AppOpenAdManager.swift index 29a086bd..df52f094 100644 --- a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/AppOpenAdManager.swift +++ b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/AppOpenAdManager.swift @@ -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 } } @@ -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 } @@ -120,7 +119,9 @@ extension AppOpenAdManager: GADFullScreenContentDelegate { isShowingAd = false print("App open ad was dismissed.") appOpenAdManagerAdDidComplete() - loadAd() + Task { + await loadAd() + } } func ad( @@ -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() + } } } diff --git a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/SplashViewController.swift b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/SplashViewController.swift index a95d7261..1cb9c4d1 100644 --- a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/SplashViewController.swift +++ b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/SplashViewController.swift @@ -81,7 +81,9 @@ class SplashViewController: UIViewController, AppOpenAdManagerDelegate { GADMobileAds.sharedInstance().start() // Load an ad. - AppOpenAdManager.shared.loadAd() + Task { + await AppOpenAdManager.shared.loadAd() + } } } diff --git a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift index 0329ff4a..8c0b89e1 100644 --- a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift +++ b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift @@ -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() + } } } @@ -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)") } } @@ -251,7 +249,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate { startNewGame() if GoogleMobileAdsConsentManager.shared.canRequestAds { - loadInterstitial() + Task { + await loadInterstitial() + } } } diff --git a/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift b/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift index 8a131d0d..43309495 100644 --- a/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift +++ b/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift @@ -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() + } } } @@ -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 } } @@ -260,7 +258,9 @@ class GameViewController: UIViewController, GADFullScreenContentDelegate { @IBAction func playAgain(_ sender: AnyObject) { startNewGame() if GoogleMobileAdsConsentManager.shared.canRequestAds { - loadRewardedInterstitialAd() + Task { + await loadRewardedInterstitialAd() + } } } diff --git a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/Base.lproj/Main.storyboard b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/Base.lproj/Main.storyboard index b9a6d611..da6fe841 100644 --- a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/Base.lproj/Main.storyboard +++ b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/Base.lproj/Main.storyboard @@ -1,9 +1,9 @@ - + - + @@ -101,6 +101,7 @@ + diff --git a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift index 29a491b0..9850cbec 100644 --- a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift +++ b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift @@ -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)") } } @@ -257,7 +256,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate { @IBAction func playAgain(_ sender: AnyObject) { startNewGame() if GoogleMobileAdsConsentManager.shared.canRequestAds { - loadRewardedAd() + Task { + await loadRewardedAd() + } } } diff --git a/Swift/admob/AppOpenExample/AppOpenExample/AppOpenAdManager.swift b/Swift/admob/AppOpenExample/AppOpenExample/AppOpenAdManager.swift index 79626dba..3e764d16 100644 --- a/Swift/admob/AppOpenExample/AppOpenExample/AppOpenAdManager.swift +++ b/Swift/admob/AppOpenExample/AppOpenExample/AppOpenAdManager.swift @@ -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 } } @@ -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 } @@ -120,7 +119,9 @@ extension AppOpenAdManager: GADFullScreenContentDelegate { isShowingAd = false print("App open ad was dismissed.") appOpenAdManagerAdDidComplete() - loadAd() + Task { + await loadAd() + } } func ad( @@ -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() + } } } diff --git a/Swift/admob/AppOpenExample/AppOpenExample/SplashViewController.swift b/Swift/admob/AppOpenExample/AppOpenExample/SplashViewController.swift index a95d7261..1cb9c4d1 100644 --- a/Swift/admob/AppOpenExample/AppOpenExample/SplashViewController.swift +++ b/Swift/admob/AppOpenExample/AppOpenExample/SplashViewController.swift @@ -81,7 +81,9 @@ class SplashViewController: UIViewController, AppOpenAdManagerDelegate { GADMobileAds.sharedInstance().start() // Load an ad. - AppOpenAdManager.shared.loadAd() + Task { + await AppOpenAdManager.shared.loadAd() + } } } diff --git a/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift b/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift index 4fd49744..ac5ef59a 100644 --- a/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift +++ b/Swift/admob/InterstitialExample/InterstitialExample/ViewController.swift @@ -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() + } } } @@ -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)") } } @@ -252,7 +250,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate { startNewGame() if GoogleMobileAdsConsentManager.shared.canRequestAds { - loadInterstitial() + Task { + await loadInterstitial() + } } } diff --git a/Swift/admob/RewardedInterstitialExample/RewardedInterstitialExample/GameViewController.swift b/Swift/admob/RewardedInterstitialExample/RewardedInterstitialExample/GameViewController.swift index bc26b78e..a53da3ea 100644 --- a/Swift/admob/RewardedInterstitialExample/RewardedInterstitialExample/GameViewController.swift +++ b/Swift/admob/RewardedInterstitialExample/RewardedInterstitialExample/GameViewController.swift @@ -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() + } } } @@ -129,18 +131,14 @@ class GameViewController: UIViewController, GADFullScreenContentDelegate { repeats: true) } - private func loadRewardedInterstitialAd() { - let request = GADRequest() - GADRewardedInterstitialAd.load( - withAdUnitID: "ca-app-pub-3940256099942544/6978759866", 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: "ca-app-pub-3940256099942544/6978759866", request: GADRequest()) + rewardedInterstitialAd?.fullScreenContentDelegate = self + } catch { + print("Failed to load rewarded interstitial ad with error: \(error.localizedDescription)") + playAgainButton.isHidden = false } } @@ -260,7 +258,9 @@ class GameViewController: UIViewController, GADFullScreenContentDelegate { @IBAction func playAgain(_ sender: AnyObject) { startNewGame() if GoogleMobileAdsConsentManager.shared.canRequestAds { - loadRewardedInterstitialAd() + Task { + await loadRewardedInterstitialAd() + } } } diff --git a/Swift/admob/RewardedVideoExample/RewardedVideoExample/ViewController.swift b/Swift/admob/RewardedVideoExample/RewardedVideoExample/ViewController.swift index 66f6d2fc..498f86c5 100644 --- a/Swift/admob/RewardedVideoExample/RewardedVideoExample/ViewController.swift +++ b/Swift/admob/RewardedVideoExample/RewardedVideoExample/ViewController.swift @@ -120,21 +120,19 @@ 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: "ca-app-pub-3940256099942544/1712485313", request: GADRequest() - ) { (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: "ca-app-pub-3940256099942544/1712485313", request: GADRequest()) + rewardedAd?.fullScreenContentDelegate = self + } catch { + print("Rewarded ad failed to load with error: \(error.localizedDescription)") } } @@ -257,7 +255,9 @@ class ViewController: UIViewController, GADFullScreenContentDelegate { @IBAction func playAgain(_ sender: AnyObject) { startNewGame() if GoogleMobileAdsConsentManager.shared.canRequestAds { - loadRewardedAd() + Task { + await loadRewardedAd() + } } }