Implementing Google User Messaging Platform in SwiftUI app

464 views
Skip to first unread message

Fabio Pohl

unread,
Jul 16, 2023, 12:49:54 PM7/16/23
to Google Mobile Ads SDK Developers
Hello everyone,

as I am building apps for the german market, I need to implement the Google User Messaging Platform into my app to ask for consent.

Now, that I followed the documentation here: https://developers.google.com/admob/ios/privacy I think that the documentation is for UIKit apps only. Could that be?

I am building my apps in SwiftUI and as I tried to implement the UMP in my AppDelegate, I stumbled upon the following error: Cannot convert value of type 'AppDelegate' to expected argument type 'UIViewController'.

So I assume that the loadForm() function in the documentation was written for UIKit apps.

For any help implementing the UMP in SwiftUI apps I would be very grateful!

Mobile Ads SDK Forum Advisor

unread,
Jul 17, 2023, 4:39:35 AM7/17/23
to google-adm...@googlegroups.com
Hello,

Thank you for reaching out to us.

With regard to your concern, please do note that iOS UMP SDK does not fully supports SwiftUI which is the reason why you're getting the reported error. I've submitted a feature request for this. In the meantime, you may keep an eye on iOS UMP SDK release notes here: https://developers.google.com/admob/ios/privacy/download.
 
This message is in relation to case "ref:_00D1U1174p._5004Q2n8G6E:ref"

Thanks,
 
Google Logo Mobile Ads SDK Team


Message has been deleted

Mathieu Dauré

unread,
Jul 20, 2023, 2:56:27 PM7/20/23
to Google Mobile Ads SDK Developers
Hi !

I had the same question and found a simple solution to just wrap the UMP Code inside a UIViewController "wrapper"

Here is the code based on the official documentation :

class UMPViewController: UIViewController {
    var canLoadAdsCallback: (() -> Void)?
   
    init() {
        super.init(nibName: nil, bundle: nil)
        // Perform any initialization logic here
        let parameters = UMPRequestParameters()
        // Set tag for under age of consent. Here false means users are not under age.
        parameters.tagForUnderAgeOfConsent = false
       
        // Request an update to the consent information.
        UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(
            with: parameters,
            completionHandler: { error in
                if error != nil {
                    // Handle the error.
                } else {
                    // The consent information state was updated.
                    // You are now ready to check if a form is
                    // available.
                    let formStatus = UMPConsentInformation.sharedInstance.formStatus
                    if formStatus == UMPFormStatus.available {
                        self.loadForm()
                    }
                }
            })
       
    }
   
    func loadForm() {
        UMPConsentForm.load(completionHandler: { form, loadError in
            if loadError != nil {
                // Handle the error.
            } else {
                // Present the form. You can also hold on to the reference to present
                // later.
                if UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatus.required {
                    form?.present(
                        from: self,
                        completionHandler: { dismissError in
                            if UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatus.obtained {
                                // App can start requesting ads.
                                if let callback = self.canLoadAdsCallback {
                                    callback()
                                }
                            }
                            // Handle dismissal by reloading form.
                            self.loadForm();
                        })
                } else {
                    // Keep the form available for changes to user consent.
                }
            }
        })
    }
   
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   
    // Your existing code and UI setup here
}

struct UMPWrapper: UIViewControllerRepresentable {
    var canLoadAdsCallback: (() -> Void)?
   
    func makeUIViewController(context: Context) -> UMPViewController {
        let umpViewController = UMPViewController()
        umpViewController.canLoadAdsCallback = canLoadAdsCallback
        return umpViewController
    }
   
    func updateUIViewController(_ uiViewController: UMPViewController, context: Context) {
       
    }
}

And then you can use it on your Main view by embedding it in a Zstack for example :

    var body: some View {
        ZStack {
            MainContent()
            UMPWrapper(canLoadAdsCallback: {
                debugPrint("Can load ads now")
            })
            .background(.clear)
            .disabled(true)
        }
    }

Hope that will helps :)

Fabio Pohl

unread,
Jul 20, 2023, 4:45:43 PM7/20/23
to Google Mobile Ads SDK Developers
Brilliant! I got it running, thank you!

Mobile Ads SDK Forum Advisor

unread,
Jul 21, 2023, 1:43:01 AM7/21/23
to mathie...@gmail.com, google-adm...@googlegroups.com
Hi Mathieu,

With regard to your concern, please do note that iOS UMP SDK does not fully supports SwiftUI. A feature request has been submitted regarding this and this is now under discussion. Kindly note that all feature request is not guaranteed to be supported anytime soon. In the meantime, you may keep an eye on iOS UMP SDK release notes here: https://developers.google.com/admob/ios/privacy/download.
 
This message is in relation to case "ref:_00D1U1174p._5004Q2nA2Zo:ref"


Thanks,
 
Google Logo Mobile Ads SDK Team

 

Test Dvd

unread,
Jul 21, 2023, 4:39:04 AM7/21/23
to Google Mobile Ads SDK Developers
Hi Fabio,
Have you been able to implement the ump sdk and the att and have Apple accepted the update?

Thanks.

Fabio Pohl

unread,
Jul 21, 2023, 8:35:08 AM7/21/23
to Google Mobile Ads SDK Developers
Hi, i have been able to implement the ump sdk and it seems to be working well. But I did not published an update yet. Will keep you up to date, when I submit the update.

Test Dvd

unread,
Jul 21, 2023, 8:43:51 AM7/21/23
to Google Mobile Ads SDK Developers
Hi Fabio,
Yes, please let us know what Apple tells you if they accept the app or not.

By the way, what is your sequence?

Do you show the rdpg first and if they accept, do you show the att?
If they reject, do you go directly to the app?

Thank you.

Fabio Pohl

unread,
Jul 21, 2023, 8:46:55 AM7/21/23
to Google Mobile Ads SDK Developers
Actually, I am not that far yet. I thought that UMP and AdMob solve that themself, but regarding to your question I think they don't. But I think I would do it, how you described it.
Reply all
Reply to author
Forward
0 new messages