Skip to content

Commit

Permalink
Using async/await for UMP SDK in SwiftUIDemo
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 684086924
  • Loading branch information
Justin Malandruccolo authored and copybara-github committed Oct 9, 2024
1 parent 902a2f4 commit 2a4287e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class GoogleMobileAdsConsentManager: NSObject {
func gatherConsent(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 @@ -53,18 +53,21 @@ class GoogleMobileAdsConsentManager: NSObject {
return consentGatheringComplete(requestConsentError)
}

UMPConsentForm.loadAndPresentIfRequired(from: nil) {
loadAndPresentError in

// Consent has been gathered.
consentGatheringComplete(loadAndPresentError)
Task { @MainActor in
do {
try await UMPConsentForm.loadAndPresentIfRequired(from: nil)
// 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(completionHandler: @escaping (Error?) -> Void) {
UMPConsentForm.presentPrivacyOptionsForm(from: nil, completionHandler: completionHandler)
@MainActor func presentPrivacyOptionsForm() async throws {
try await UMPConsentForm.presentPrivacyOptionsForm(from: nil)
}

/// Method to initialize the Google Mobile Ads SDK. The SDK should only be initialized once.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ struct MenuView: View {
.navigationTitle("Menu")
.toolbar {
Button("Privacy Settings") {
GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm { formError in
guard let formError else { return }

formErrorDescription = formError.localizedDescription
showPrivacyOptionsAlert = true
Task {
do {
try await GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm()
} catch {
formErrorDescription = error.localizedDescription
showPrivacyOptionsAlert = true
}
}
}
.disabled(isPrivacyOptionsButtonDisabled)
Expand Down

0 comments on commit 2a4287e

Please sign in to comment.