如何通过iOS应用设置中的开关让用户更改广告同意(个性化与否)?

时间:2019-07-02 02:19:04

标签: ios swift admob personalization

我试图使用AdMob和Firebase Analytics通过允许用户打开/关闭个性化广告的开关来更改用户在“应用程序设置”中的AdMob同意类型(如果用户将开关“打开”,则他们将获得个性化广告,如果将其设置为“关闭”,则会获得非个性化广告)。 我写了一个代码,但是它给了我错误,并且还循环了。正确的方法/代码是什么?

这是我目前正在做的事情: 1-在Settings.bundle> Root.plist中添加了标识符为“ GooglePersonalizedAds”的切换开关项,以便用户在应用程序设置中打开/关闭该开关

2-在我的应用的“ info.plist”中将“ FIREBASE_ANALYTICS_COLLECTION_ENABLED”和“ GOOGLE_ANALYTICS_DEFAULT_ALLOW_AD_PERSONALIZATION_SIGNALS”添加为“布尔”属性,并将其值设置为“否”。

3-要初始化FireBase,我在AppDelegate中添加了:

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

4-在ViewController.swift的viewDidLoad()中,我添加了:

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

5-在ViewController.swift中,我添加了:

@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)
    }

当我运行该应用程序时,我在控制台中看到许多“ GooglePersonalizedAds更改为:false”,最后该应用程序因错误而崩溃。崩溃后出现的一个错误是: “线程1:EXC_BAD_ACCESS(代码= 2,地址= 0x7ffeeb86ffd8)”错误 对于“ NSUserDefaults.standardUserDefaults setObject:personalizedAdStatus forKey:PACUserDefaultsRootKey]”

我认为错误来自:“ Analytics.setAnalyticsCollectionEnabled(false)PACConsentInformation.sharedInstance.consentStatus = .nonPersonalized”或“ PACConsentInformation.sharedInstance.consentStatus = .personalized” 会更改UserDefaults,从而调用观察者和defaultsChanged(),从而创建一个循环。

如何通过使用开关(标识符:App设置中的GooglePersonalizedAds,如我所尝试的那样)将Analytics.setAnalyticsCollectionEnabled(false)PACConsentInformation.sharedInstance.consentStatus设置为.personalized或.nonPersonalized?

0 个答案:

没有答案