2

Using AdMob and Firebase Analytics, I'm trying to let users change their AdMob consent type in App's Settings by letting them turn a switch on/off for personalized ads (If users turn the switch "On", they would get personalized ads and if they turn it "Off", they would get non-personalized ads). I wrote a code, but it gives me errors and it also loops. What is the correct way/code to do so?

Here is what I am currently doing: 1- Added a toggle switch item in Settings.bundle>Root.plist with identifier "GooglePersonalizedAds" to let user turn the switch on/off in the app's setting

2- Added "FIREBASE_ANALYTICS_COLLECTION_ENABLED" and "GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_PERSONALIZATION_SIGNALS" as "boolean" properties in my app's "info.plist" and set their value to "NO".

3- To initialize FireBase, In AppDelegate I added:

FirebaseConfiguration.shared.setLoggerLevel(.min)
FirebaseApp.configure()

4- In the viewDidLoad() of the ViewController.swift, I added:

registerSettingsBundle()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.defaultsChanged), name: UserDefaults.didChangeNotification, object: nil)
defaultsChanged()

5- In ViewController.swift, I added:

@objc func defaultsChanged(){
let userDefault = UserDefaults.standard   //Fetch value from Settings.bundle        

if !purchasedUser {   

if userDefault.bool(forKey: "GooglePersonalizedAds") {

                print("GooglePersonalizedAds changed to: \(userDefault.bool(forKey: "GooglePersonalizedAds"))")
Analytics.setUserProperty("true", forName: AnalyticsUserPropertyAllowAdPersonalizationSignals)
self.view.backgroundColor = UIColor.red // To Test the implementation of personalized/non-personalized ads switch in App's settings
Analytics.setAnalyticsCollectionEnabled(true)
PACConsentInformation.sharedInstance.consentStatus = .personalized
let request = GADRequest()
let extras = GADExtras()
extras.additionalParameters = ["npa": "0"]
request.testDevices = [testDeviceString] 
request.register(extras)
} 

else if !userDefault.bool(forKey: "GooglePersonalizedAds") {

print("GooglePersonalizedAds changed to: \(userDefault.bool(forKey: "GooglePersonalizedAds"))")
Analytics.setUserProperty("false", forName: AnalyticsUserPropertyAllowAdPersonalizationSignals)
self.view.backgroundColor = UIColor.green // To Test the implementation of personalized/non-personalized ads switch in App's settings
Analytics.setAnalyticsCollectionEnabled(false)
PACConsentInformation.sharedInstance.consentStatus = .nonPersonalized
let request = DFPRequest()
let extras = GADExtras()
extras.additionalParameters = ["npa": "1"]
request.testDevices = [testDeviceString]
request.register(extras)
}

} else if purchasedUser {

 print("GooglePersonalizedAds changed to: \(userDefault.bool(forKey: "GooglePersonalizedAds"))")
Analytics.setUserProperty("false", forName: AnalyticsUserPropertyAllowAdPersonalizationSignals)
self.view.backgroundColor = UIColor.green
Analytics.setAnalyticsCollectionEnabled(false)PACConsentInformation.sharedInstance.consentStatus = .nonPersonalized

}    
registerSettingsBundle()
}




func registerSettingsBundle(){
        let appDefaults = [String:AnyObject]()
        UserDefaults.standard.register(defaults: appDefaults)
    }

When I run the app, I get many "GooglePersonalizedAds changed to: false" printed in the console and finally the app crashes with an error. One error that I got after crash was: "Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffeeb86ffd8)" error for "NSUserDefaults.standardUserDefaults setObject:personalizedAdStatus forKey:PACUserDefaultsRootKey]"

I think the errors come from: "Analytics.setAnalyticsCollectionEnabled(false)PACConsentInformation.sharedInstance.consentStatus = .nonPersonalized" or "PACConsentInformation.sharedInstance.consentStatus = .personalized" which change the UserDefaults, and therefore call the observer and defaultsChanged() and so, create a loop.

How may I set the Analytics.setAnalyticsCollectionEnabled(false)PACConsentInformation.sharedInstance.consentStatus to .personalized or .nonPersonalized by using a switch (identifier: GooglePersonalizedAds in App's setting, as tries in my question?

0 Answers0