Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using nil for UMP SDK UIViewController parameter in SwiftUIDemo #462

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Swift/advanced/SwiftUIDemo/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PODS:
- Google-Mobile-Ads-SDK (11.8.0):
- GoogleUserMessagingPlatform (>= 1.1)
- GoogleUserMessagingPlatform (2.5.0)
- GoogleUserMessagingPlatform (2.6.0)

DEPENDENCIES:
- Google-Mobile-Ads-SDK
Expand All @@ -14,7 +14,7 @@ SPEC REPOS:

SPEC CHECKSUMS:
Google-Mobile-Ads-SDK: c1c53687b572122c5b0bdaf354335bbf40fd81ef
GoogleUserMessagingPlatform: 6b4f48a370e77ce121d034c908cc6ee4fdafaf13
GoogleUserMessagingPlatform: 0c3a08353e53ce8c2feab7addd0b652cde522450

PODFILE CHECKSUM: e9e7b597a08c2a8bd61f9bf568587b1a7a6bcdd6

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ 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,
consentGatheringComplete: @escaping (Error?) -> Void
) {
func gatherConsent(consentGatheringComplete: @escaping (Error?) -> Void) {
let parameters = UMPRequestParameters()

//For testing purposes, you can force a UMPDebugGeography of EEA or not EEA.
Expand All @@ -56,7 +53,7 @@ class GoogleMobileAdsConsentManager: NSObject {
return consentGatheringComplete(requestConsentError)
}

UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) {
UMPConsentForm.loadAndPresentIfRequired(from: nil) {
loadAndPresentError in

// Consent has been gathered.
Expand All @@ -66,11 +63,8 @@ class GoogleMobileAdsConsentManager: NSObject {
}

/// 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)
func presentPrivacyOptionsForm(completionHandler: @escaping (Error?) -> Void) {
UMPConsentForm.presentPrivacyOptionsForm(from: nil, completionHandler: completionHandler)
}

/// 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 @@ -11,15 +11,8 @@ struct MenuView: View {

@State private var isMenuItemDisabled = true
@State private var isPrivacyOptionsButtonDisabled = true
@State private var hasViewAppeared = false
@State private var showPrivacyOptionsAlert = false
@State private var formErrorDescription: String?
private let formViewControllerRepresentable = FormViewControllerRepresentable()

var formViewControllerRepresentableView: some View {
formViewControllerRepresentable
.frame(width: .zero, height: .zero)
}

var body: some View {
NavigationView {
Expand All @@ -30,12 +23,9 @@ struct MenuView: View {
.disabled(isMenuItemDisabled)
}
.navigationTitle("Menu")
.background(formViewControllerRepresentableView)
.toolbar {
Button("Privacy Settings") {
GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm(
from: formViewControllerRepresentable.viewController
) { (formError) in
GoogleMobileAdsConsentManager.shared.presentPrivacyOptionsForm { formError in
guard let formError else { return }

formErrorDescription = formError.localizedDescription
Expand All @@ -51,13 +41,7 @@ struct MenuView: View {
}
}
.onAppear {
guard !hasViewAppeared else { return }
hasViewAppeared = true

GoogleMobileAdsConsentManager.shared.gatherConsent(
from: formViewControllerRepresentable.viewController
) { (consentError) in

GoogleMobileAdsConsentManager.shared.gatherConsent { consentError in
if let consentError {
// Consent gathering failed.
print("Error: \(consentError.localizedDescription)")
Expand All @@ -76,24 +60,6 @@ struct MenuView: View {
}
}

/// Helper to present UMP consent form
///
/// A `UIViewControllerRepresentable` that exposes access to a `UIViewController` reference in
/// SwiftUI.
///
/// `FormViewControllerRepresentable` needs to be included as part of the view hierarchy because
/// to present the UMP consent form, `canPresent(fromRootViewController:)` requires the
/// presenting view controller’s window value to not be nil.
private struct FormViewControllerRepresentable: UIViewControllerRepresentable {
let viewController = UIViewController()

func makeUIViewController(context: Context) -> some UIViewController {
return viewController
}

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}

struct MenuView_Previews: PreviewProvider {
static var previews: some View {
MenuView()
Expand Down
Loading