Skip to content

Commit

Permalink
Refactored Swift samples to use async/await
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 688562357
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Oct 27, 2024
1 parent f11bd13 commit ea41593
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 212 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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.
Expand Down 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
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)
}
}
Loading

0 comments on commit ea41593

Please sign in to comment.