From 6908d4133cd9385ed82b1415bde458352bbeb5eb Mon Sep 17 00:00:00 2001 From: Justin Malandruccolo Date: Wed, 9 Oct 2024 11:15:19 -0700 Subject: [PATCH] Updated APIs to use async await PiperOrigin-RevId: 684105010 --- .../GoogleMobileAdsConsentManager.swift | 26 ++++++------ .../BannerExample/ViewController.swift | 42 ++++++++++--------- 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/Swift/admob/BannerExample/BannerExample/GoogleMobileAdsConsentManager.swift b/Swift/admob/BannerExample/BannerExample/GoogleMobileAdsConsentManager.swift index 3ff8782d..091ee3d6 100644 --- a/Swift/admob/BannerExample/BannerExample/GoogleMobileAdsConsentManager.swift +++ b/Swift/admob/BannerExample/BannerExample/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/admob/BannerExample/BannerExample/ViewController.swift b/Swift/admob/BannerExample/BannerExample/ViewController.swift index e9844500..71c36eaf 100644 --- a/Swift/admob/BannerExample/BannerExample/ViewController.swift +++ b/Swift/admob/BannerExample/BannerExample/ViewController.swift @@ -34,7 +34,7 @@ class ViewController: UIViewController, GADBannerViewDelegate { bannerView.rootViewController = self bannerView.delegate = self - GoogleMobileAdsConsentManager.shared.gatherConsent(from: self) { [weak self] (consentError) in + GoogleMobileAdsConsentManager.shared.gatherConsent(from: self) { [weak self] consentError in guard let self else { return } if let consentError { @@ -79,30 +79,32 @@ 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() + } 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) + } } } /// 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 { + // There was an issue and the inspector was not displayed. + let alertController = UIAlertController( + title: error.localizedDescription, message: "Please try again later.", + preferredStyle: .alert) + alertController.addAction(UIAlertAction(title: "OK", style: .cancel)) + present(alertController, animated: true) + } } }