3

So Ive been trying for over a week to get the google user messaging platform to work (a question that's going to pop up all over the place in the next few weeks/mponths). The problem is that the start guid is written in obj C and I can't understand it! Ive managed to insert the obj C code into my swift project and it runs but causes an exception when it tries to present the form. I think this is because its a .m file and its trying to present the form from self and as 'self' isn't a view controller then it crashes? The obj C Code (as recommended in the google site) is here:

#import "CustomObject.h"

@implementation CustomObject 

- (void) someMethod {
// Create a UMPRequestParameters object.
UMPRequestParameters *parameters = [[UMPRequestParameters alloc] init];
// Set tag for under age of consent. Here NO means users are not under age.
parameters.tagForUnderAgeOfConsent = NO;

// Request an update to the consent information.
[UMPConsentInformation.sharedInstance
    requestConsentInfoUpdateWithParameters:parameters
                         completionHandler:^(NSError *_Nullable error) {
                           if (error) {
                             // Handle the error.
                           } else {
                             // The consent information state was updated.
                             // You are now ready to check if a form is
                             // available.
                            UMPFormStatus formStatus = UMPConsentInformation.sharedInstance.formStatus;
                            if (formStatus == UMPFormStatusAvailable) {
                            [self loadForm];
                            }
                           }
                         }];
}
    -(void) loadForm {
        [UMPConsentForm loadWithCompletionHandler:^(UMPConsentForm *form, NSError *loadError) {
               if (loadError) {
                 // Handle the error
               } else {
                   // Present the form. You can also hold on to the reference to present
                   // later.
                   if (UMPConsentInformation.sharedInstance.consentStatus ==
                       UMPConsentStatusRequired) {
                     [form
                      presentFromViewController: self
                                 completionHandler:^(NSError *_Nullable dismissError) {
                                   if (UMPConsentInformation.sharedInstance.consentStatus ==
                                       UMPConsentStatusObtained) {
                                     // App can start requesting ads.
                                   }

                                 }];
                   } else {
                     // Keep the form available for changes to user consent.
                   }
               }
             }];
    }

@end

Im pulling my hair out here. What I really need is either a. A conversion of this code into swift so I can understand it OR b. can anyone suggest what I need to do to the presentfromviewcontroller function to stop it throwing an exception and present a form?

Thanks.....

RobD
  • 131
  • 3

1 Answers1

1

Ok, so having had no responses to my question I've finally managed to get it working by translating the obj c into swift. Thought it would be helpful to post it here in case anyone else is struggling with the same issue. This is the code I came up with. Add the 2 functions into your view controller and call the first one from viewdidload. The commented out bit is the debug set that allows you to try different geolocations etc to make sure its working. This code works but Im not that good at swift so please feel free to point out mistakes or correct it. Don't forget to add import userMessagingPlatform at the top...

  func setPermissions() {
  
    //  UMPConsentInformation.sharedInstance.reset()  // always render a consent form
    //  let debugSettings = UMPDebugSettings()
    //  debugSettings.testDeviceIdentifiers = ["your test ID here"]
    //  debugSettings.geography = .notEEA
    //  parameters.debugSettings = debugSettings
    
    let parameters = UMPRequestParameters()
    parameters.tagForUnderAgeOfConsent = false
    UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters, completionHandler: {(error) in
    if let error = error as NSError? {
       // deal with error
    } else { // now check for available form and load it
        let formStatus = UMPConsentInformation.sharedInstance.formStatus
        if formStatus == .available {
            self.loadForm()
            }
        }
    })
}
func loadForm() {
    UMPConsentForm.load(completionHandler: { (form, loadError) in
        if let error = loadError as NSError? {
           // deal with error loading form
        } else {
            if UMPConsentInformation.sharedInstance.consentStatus == .required {
            form?.present(from: self, completionHandler: {(error) in
                if UMPConsentInformation.sharedInstance.consentStatus == .obtained {
                    // OK to serve ads
                } else {
                    // not OK to serve ads
                    }
                })
            }
        }
    })
}
RobD
  • 131
  • 3
  • One note: careful, consentStatus ".obtained" means the user has taken a decision, not that they have decided to allow personalized ad. We then need to check the consentType (https://developers.google.com/admob/ump/ios/api/reference/Enums/UMPConsentType) – Kqtr Dec 11 '22 at 17:40