From ea415930f2c0685fe453150fe38063d0cfd3908d Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Tue, 22 Oct 2024 08:35:12 -0700 Subject: [PATCH] Refactored Swift samples to use async/await PiperOrigin-RevId: 688562357 --- .../GoogleMobileAdsConsentManager.swift | 28 +++++----- .../MainViewController.swift | 42 ++++++++------- .../GoogleMobileAdsConsentManager.swift | 28 +++++----- .../ViewController.swift | 42 ++++++++------- .../GoogleMobileAdsConsentManager.swift | 28 +++++----- .../ViewController.swift | 42 ++++++++------- .../GoogleMobileAdsConsentManager.swift | 26 +++++----- .../ViewController.swift | 52 +++++++++---------- .../GameViewController.swift | 52 +++++++++---------- .../GoogleMobileAdsConsentManager.swift | 28 +++++----- .../GoogleMobileAdsConsentManager.swift | 28 +++++----- .../ViewController.swift | 52 +++++++++---------- 12 files changed, 236 insertions(+), 212 deletions(-) diff --git a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/GoogleMobileAdsConsentManager.swift b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/GoogleMobileAdsConsentManager.swift index 037fe058..091ee3d6 100644 --- a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/GoogleMobileAdsConsentManager.swift +++ b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/GoogleMobileAdsConsentManager.swift @@ -1,5 +1,5 @@ // -// Copyright 2023 Google LLC +// Copyright (C) 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. @@ -37,12 +37,12 @@ class GoogleMobileAdsConsentManager: NSObject { /// Helper method to call the UMP SDK methods to request consent information and load/present a /// consent form if necessary. func gatherConsent( - from consentFormPresentationviewController: UIViewController, + from viewController: UIViewController? = nil, consentGatheringComplete: @escaping (Error?) -> Void ) { let parameters = UMPRequestParameters() - //For testing purposes, you can force a UMPDebugGeography of EEA or not EEA. + // For testing purposes, you can use UMPDebugGeography to simulate a location. let debugSettings = UMPDebugSettings() // debugSettings.geography = UMPDebugGeography.EEA parameters.debugSettings = debugSettings @@ -54,20 +54,22 @@ class GoogleMobileAdsConsentManager: NSObject { return consentGatheringComplete(requestConsentError) } - UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) { - loadAndPresentError in - - // Consent has been gathered. - consentGatheringComplete(loadAndPresentError) + Task { @MainActor in + do { + try await UMPConsentForm.loadAndPresentIfRequired(from: viewController) + // Consent has been gathered. + consentGatheringComplete(nil) + } catch { + consentGatheringComplete(error) + } } } } /// Helper method to call the UMP SDK method to present the privacy options form. - func presentPrivacyOptionsForm( - from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void - ) { - UMPConsentForm.presentPrivacyOptionsForm( - from: viewController, completionHandler: completionHandler) + @MainActor func presentPrivacyOptionsForm(from viewController: UIViewController? = nil) + async throws + { + try await UMPConsentForm.presentPrivacyOptionsForm(from: viewController) } } diff --git a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/MainViewController.swift b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/MainViewController.swift index dbefba67..fdf32cd7 100644 --- a/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/MainViewController.swift +++ b/Swift/admanager/AdManagerAppOpenExample/AdManagerAppOpenExample/MainViewController.swift @@ -34,30 +34,34 @@ class MainViewController: UIViewController { /// Handle changes to user consent. @IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) { - GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) { - [weak self] (formError) in - guard let self, let formError else { return } - - let alertController = UIAlertController( - title: formError.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction( + UIAlertAction( + title: "OK", style: .cancel, + handler: nil)) + present(alertController, animated: true) + } } } /// Handle ad inspector launch. @IBAction func adInspectorTapped(_ sender: UIBarButtonItem) { - GADMobileAds.sharedInstance().presentAdInspector(from: self) { - // Error will be non-nil if there was an issue and the inspector was not displayed. - [weak self] error in - guard let self, let error else { return } - - let alertController = UIAlertController( - title: error.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GADMobileAds.sharedInstance().presentAdInspector(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + present(alertController, animated: true) + } } } } diff --git a/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/GoogleMobileAdsConsentManager.swift b/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/GoogleMobileAdsConsentManager.swift index 037fe058..091ee3d6 100644 --- a/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/GoogleMobileAdsConsentManager.swift +++ b/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/GoogleMobileAdsConsentManager.swift @@ -1,5 +1,5 @@ // -// Copyright 2023 Google LLC +// Copyright (C) 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. @@ -37,12 +37,12 @@ class GoogleMobileAdsConsentManager: NSObject { /// Helper method to call the UMP SDK methods to request consent information and load/present a /// consent form if necessary. func gatherConsent( - from consentFormPresentationviewController: UIViewController, + from viewController: UIViewController? = nil, consentGatheringComplete: @escaping (Error?) -> Void ) { let parameters = UMPRequestParameters() - //For testing purposes, you can force a UMPDebugGeography of EEA or not EEA. + // For testing purposes, you can use UMPDebugGeography to simulate a location. let debugSettings = UMPDebugSettings() // debugSettings.geography = UMPDebugGeography.EEA parameters.debugSettings = debugSettings @@ -54,20 +54,22 @@ class GoogleMobileAdsConsentManager: NSObject { return consentGatheringComplete(requestConsentError) } - UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) { - loadAndPresentError in - - // Consent has been gathered. - consentGatheringComplete(loadAndPresentError) + Task { @MainActor in + do { + try await UMPConsentForm.loadAndPresentIfRequired(from: viewController) + // Consent has been gathered. + consentGatheringComplete(nil) + } catch { + consentGatheringComplete(error) + } } } } /// Helper method to call the UMP SDK method to present the privacy options form. - func presentPrivacyOptionsForm( - from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void - ) { - UMPConsentForm.presentPrivacyOptionsForm( - from: viewController, completionHandler: completionHandler) + @MainActor func presentPrivacyOptionsForm(from viewController: UIViewController? = nil) + async throws + { + try await UMPConsentForm.presentPrivacyOptionsForm(from: viewController) } } diff --git a/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/ViewController.swift b/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/ViewController.swift index 92fa79e6..d71d0d44 100644 --- a/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/ViewController.swift +++ b/Swift/admanager/AdManagerBannerExample/AdManagerBannerExample/ViewController.swift @@ -78,30 +78,34 @@ class ViewController: UIViewController, GADBannerViewDelegate { /// Handle changes to user consent. @IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) { - GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) { - [weak self] (formError) in - guard let self, let formError else { return } - - let alertController = UIAlertController( - title: formError.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction( + UIAlertAction( + title: "OK", style: .cancel, + handler: nil)) + present(alertController, animated: true) + } } } /// Handle ad inspector launch. @IBAction func adInspectorTapped(_ sender: UIBarButtonItem) { - GADMobileAds.sharedInstance().presentAdInspector(from: self) { - // Error will be non-nil if there was an issue and the inspector was not displayed. - [weak self] error in - guard let self, let error else { return } - - let alertController = UIAlertController( - title: error.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GADMobileAds.sharedInstance().presentAdInspector(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + present(alertController, animated: true) + } } } diff --git a/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/GoogleMobileAdsConsentManager.swift b/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/GoogleMobileAdsConsentManager.swift index 037fe058..091ee3d6 100644 --- a/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/GoogleMobileAdsConsentManager.swift +++ b/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/GoogleMobileAdsConsentManager.swift @@ -1,5 +1,5 @@ // -// Copyright 2023 Google LLC +// Copyright (C) 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. @@ -37,12 +37,12 @@ class GoogleMobileAdsConsentManager: NSObject { /// Helper method to call the UMP SDK methods to request consent information and load/present a /// consent form if necessary. func gatherConsent( - from consentFormPresentationviewController: UIViewController, + from viewController: UIViewController? = nil, consentGatheringComplete: @escaping (Error?) -> Void ) { let parameters = UMPRequestParameters() - //For testing purposes, you can force a UMPDebugGeography of EEA or not EEA. + // For testing purposes, you can use UMPDebugGeography to simulate a location. let debugSettings = UMPDebugSettings() // debugSettings.geography = UMPDebugGeography.EEA parameters.debugSettings = debugSettings @@ -54,20 +54,22 @@ class GoogleMobileAdsConsentManager: NSObject { return consentGatheringComplete(requestConsentError) } - UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) { - loadAndPresentError in - - // Consent has been gathered. - consentGatheringComplete(loadAndPresentError) + Task { @MainActor in + do { + try await UMPConsentForm.loadAndPresentIfRequired(from: viewController) + // Consent has been gathered. + consentGatheringComplete(nil) + } catch { + consentGatheringComplete(error) + } } } } /// Helper method to call the UMP SDK method to present the privacy options form. - func presentPrivacyOptionsForm( - from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void - ) { - UMPConsentForm.presentPrivacyOptionsForm( - from: viewController, completionHandler: completionHandler) + @MainActor func presentPrivacyOptionsForm(from viewController: UIViewController? = nil) + async throws + { + try await UMPConsentForm.presentPrivacyOptionsForm(from: viewController) } } diff --git a/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/ViewController.swift b/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/ViewController.swift index 983a4aab..0e528462 100644 --- a/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/ViewController.swift +++ b/Swift/admanager/AdManagerCustomRenderingExample/AdManagerCustomRenderingExample/ViewController.swift @@ -64,30 +64,34 @@ class ViewController: UIViewController { /// Handle changes to user consent. @IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) { - GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) { - [weak self] formError in - guard let self, let formError else { return } - - let alertController = UIAlertController( - title: formError.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction( + UIAlertAction( + title: "OK", style: .cancel, + handler: nil)) + present(alertController, animated: true) + } } } /// Handle ad inspector launch. @IBAction func adInspectorTapped(_ sender: UIBarButtonItem) { - GADMobileAds.sharedInstance().presentAdInspector(from: self) { - // Error will be non-nil if there was an issue and the inspector was not displayed. - [weak self] error in - guard let self, let error else { return } - - let alertController = UIAlertController( - title: error.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GADMobileAds.sharedInstance().presentAdInspector(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + present(alertController, animated: true) + } } } diff --git a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/GoogleMobileAdsConsentManager.swift b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/GoogleMobileAdsConsentManager.swift index 3ff8782d..091ee3d6 100644 --- a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/GoogleMobileAdsConsentManager.swift +++ b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/GoogleMobileAdsConsentManager.swift @@ -37,12 +37,12 @@ class GoogleMobileAdsConsentManager: NSObject { /// Helper method to call the UMP SDK methods to request consent information and load/present a /// consent form if necessary. func gatherConsent( - from consentFormPresentationviewController: UIViewController, + from viewController: UIViewController? = nil, consentGatheringComplete: @escaping (Error?) -> Void ) { let parameters = UMPRequestParameters() - //For testing purposes, you can force a UMPDebugGeography of EEA or not EEA. + // For testing purposes, you can use UMPDebugGeography to simulate a location. let debugSettings = UMPDebugSettings() // debugSettings.geography = UMPDebugGeography.EEA parameters.debugSettings = debugSettings @@ -54,20 +54,22 @@ class GoogleMobileAdsConsentManager: NSObject { return consentGatheringComplete(requestConsentError) } - UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) { - loadAndPresentError in - - // Consent has been gathered. - consentGatheringComplete(loadAndPresentError) + Task { @MainActor in + do { + try await UMPConsentForm.loadAndPresentIfRequired(from: viewController) + // Consent has been gathered. + consentGatheringComplete(nil) + } catch { + consentGatheringComplete(error) + } } } } /// Helper method to call the UMP SDK method to present the privacy options form. - func presentPrivacyOptionsForm( - from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void - ) { - UMPConsentForm.presentPrivacyOptionsForm( - from: viewController, completionHandler: completionHandler) + @MainActor func presentPrivacyOptionsForm(from viewController: UIViewController? = nil) + async throws + { + try await UMPConsentForm.presentPrivacyOptionsForm(from: viewController) } } diff --git a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift index eeecaa03..cace905c 100644 --- a/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift +++ b/Swift/admanager/AdManagerInterstitialExample/AdManagerInterstitialExample/ViewController.swift @@ -71,37 +71,37 @@ class ViewController: UIViewController, GADFullScreenContentDelegate { /// Handle changes to user consent. @IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) { pauseGame() - - GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) { - [weak self] formError in - guard let self else { return } - guard let formError else { return self.resumeGame() } - - let alertController = UIAlertController( - title: formError.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction( - UIAlertAction( - title: "OK", style: .cancel, - handler: { _ in - self.resumeGame() - })) - self.present(alertController, animated: true) + Task { + do { + try await GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) + resumeGame() + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction( + UIAlertAction( + title: "OK", style: .cancel, + handler: { _ in + self.resumeGame() + })) + present(alertController, animated: true) + } } } /// Handle ad inspector launch. @IBAction func adInspectorTapped(_ sender: UIBarButtonItem) { - GADMobileAds.sharedInstance().presentAdInspector(from: self) { - // Error will be non-nil if there was an issue and the inspector was not displayed. - [weak self] error in - guard let self, let error else { return } - - let alertController = UIAlertController( - title: error.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GADMobileAds.sharedInstance().presentAdInspector(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + present(alertController, animated: true) + } } } diff --git a/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift b/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift index 99cc329d..4f2f0d05 100644 --- a/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift +++ b/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GameViewController.swift @@ -240,37 +240,37 @@ class GameViewController: UIViewController, GADFullScreenContentDelegate { /// Handle changes to user consent. @IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) { pauseGame() - - GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) { - [weak self] formError in - guard let self else { return } - guard let formError else { return self.resumeGame() } - - let alertController = UIAlertController( - title: formError.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction( - UIAlertAction( - title: "OK", style: .cancel, - handler: { _ in - self.resumeGame() - })) - self.present(alertController, animated: true) + Task { + do { + try await GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) + resumeGame() + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction( + UIAlertAction( + title: "OK", style: .cancel, + handler: { _ in + self.resumeGame() + })) + present(alertController, animated: true) + } } } /// Handle ad inspector launch. @IBAction func adInspectorTapped(_ sender: UIBarButtonItem) { - GADMobileAds.sharedInstance().presentAdInspector(from: self) { - // Error will be non-nil if there was an issue and the inspector was not displayed. - [weak self] error in - guard let self, let error else { return } - - let alertController = UIAlertController( - title: error.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GADMobileAds.sharedInstance().presentAdInspector(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + present(alertController, animated: true) + } } } diff --git a/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GoogleMobileAdsConsentManager.swift b/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GoogleMobileAdsConsentManager.swift index 037fe058..091ee3d6 100644 --- a/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GoogleMobileAdsConsentManager.swift +++ b/Swift/admanager/AdManagerRewardedInterstitialExample/AdManagerRewardedInterstitialExample/GoogleMobileAdsConsentManager.swift @@ -1,5 +1,5 @@ // -// Copyright 2023 Google LLC +// Copyright (C) 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. @@ -37,12 +37,12 @@ class GoogleMobileAdsConsentManager: NSObject { /// Helper method to call the UMP SDK methods to request consent information and load/present a /// consent form if necessary. func gatherConsent( - from consentFormPresentationviewController: UIViewController, + from viewController: UIViewController? = nil, consentGatheringComplete: @escaping (Error?) -> Void ) { let parameters = UMPRequestParameters() - //For testing purposes, you can force a UMPDebugGeography of EEA or not EEA. + // For testing purposes, you can use UMPDebugGeography to simulate a location. let debugSettings = UMPDebugSettings() // debugSettings.geography = UMPDebugGeography.EEA parameters.debugSettings = debugSettings @@ -54,20 +54,22 @@ class GoogleMobileAdsConsentManager: NSObject { return consentGatheringComplete(requestConsentError) } - UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) { - loadAndPresentError in - - // Consent has been gathered. - consentGatheringComplete(loadAndPresentError) + Task { @MainActor in + do { + try await UMPConsentForm.loadAndPresentIfRequired(from: viewController) + // Consent has been gathered. + consentGatheringComplete(nil) + } catch { + consentGatheringComplete(error) + } } } } /// Helper method to call the UMP SDK method to present the privacy options form. - func presentPrivacyOptionsForm( - from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void - ) { - UMPConsentForm.presentPrivacyOptionsForm( - from: viewController, completionHandler: completionHandler) + @MainActor func presentPrivacyOptionsForm(from viewController: UIViewController? = nil) + async throws + { + try await UMPConsentForm.presentPrivacyOptionsForm(from: viewController) } } diff --git a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/GoogleMobileAdsConsentManager.swift b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/GoogleMobileAdsConsentManager.swift index 037fe058..091ee3d6 100644 --- a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/GoogleMobileAdsConsentManager.swift +++ b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/GoogleMobileAdsConsentManager.swift @@ -1,5 +1,5 @@ // -// Copyright 2023 Google LLC +// Copyright (C) 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. @@ -37,12 +37,12 @@ class GoogleMobileAdsConsentManager: NSObject { /// Helper method to call the UMP SDK methods to request consent information and load/present a /// consent form if necessary. func gatherConsent( - from consentFormPresentationviewController: UIViewController, + from viewController: UIViewController? = nil, consentGatheringComplete: @escaping (Error?) -> Void ) { let parameters = UMPRequestParameters() - //For testing purposes, you can force a UMPDebugGeography of EEA or not EEA. + // For testing purposes, you can use UMPDebugGeography to simulate a location. let debugSettings = UMPDebugSettings() // debugSettings.geography = UMPDebugGeography.EEA parameters.debugSettings = debugSettings @@ -54,20 +54,22 @@ class GoogleMobileAdsConsentManager: NSObject { return consentGatheringComplete(requestConsentError) } - UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) { - loadAndPresentError in - - // Consent has been gathered. - consentGatheringComplete(loadAndPresentError) + Task { @MainActor in + do { + try await UMPConsentForm.loadAndPresentIfRequired(from: viewController) + // Consent has been gathered. + consentGatheringComplete(nil) + } catch { + consentGatheringComplete(error) + } } } } /// Helper method to call the UMP SDK method to present the privacy options form. - func presentPrivacyOptionsForm( - from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void - ) { - UMPConsentForm.presentPrivacyOptionsForm( - from: viewController, completionHandler: completionHandler) + @MainActor func presentPrivacyOptionsForm(from viewController: UIViewController? = nil) + async throws + { + try await UMPConsentForm.presentPrivacyOptionsForm(from: viewController) } } diff --git a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift index afe91214..319ce2dc 100644 --- a/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift +++ b/Swift/admanager/AdManagerRewardedVideoExample/AdManagerRewardedVideoExample/ViewController.swift @@ -215,37 +215,37 @@ class ViewController: UIViewController, GADFullScreenContentDelegate { /// Handle changes to user consent. @IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) { pauseGame() - - GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) { - [weak self] formError in - guard let self else { return } - guard let formError else { return self.resumeGame() } - - let alertController = UIAlertController( - title: formError.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction( - UIAlertAction( - title: "OK", style: .cancel, - handler: { _ in - self.resumeGame() - })) - self.present(alertController, animated: true) + Task { + do { + try await GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(from: self) + resumeGame() + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction( + UIAlertAction( + title: "OK", style: .cancel, + handler: { _ in + self.resumeGame() + })) + present(alertController, animated: true) + } } } /// Handle ad inspector launch. @IBAction func adInspectorTapped(_ sender: UIBarButtonItem) { - GADMobileAds.sharedInstance().presentAdInspector(from: self) { - // Error will be non-nil if there was an issue and the inspector was not displayed. - [weak self] error in - guard let self, let error else { return } - - let alertController = UIAlertController( - title: error.localizedDescription, message: "Please try again later.", - preferredStyle: .alert) - alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) - self.present(alertController, animated: true) + Task { + do { + try await GADMobileAds.sharedInstance().presentAdInspector(from: self) + } catch { + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + present(alertController, animated: true) + } } }