Skip to content

Commit

Permalink
Updated APIs to use async await
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684105010
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Oct 9, 2024
1 parent 2a4287e commit 6908d41
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}
42 changes: 22 additions & 20 deletions Swift/admob/BannerExample/BannerExample/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
}

Expand Down

0 comments on commit 6908d41

Please sign in to comment.