GoogleMobileAdsConsentManager.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. //
  2. // GoogleMobileAdsConsentManager.swift
  3. // PhysicalWallPaper
  4. //
  5. // Created by nkl on 2024/12/16.
  6. //
  7. import Foundation
  8. import GoogleMobileAds
  9. import UserMessagingPlatform
  10. /// Google Mobile Ads SDK 提供了用户消息平台(Google 的 IAB 认证的同意管理平台)作为在受 GDPR 影响的国家获取用户同意的解决方案之一。这是一个示例,你可以选择另一个同意管理平台来获取同意。
  11. class GoogleMobileAdsConsentManager: NSObject {
  12. static let shared = GoogleMobileAdsConsentManager()
  13. var canRequestAds: Bool {
  14. return UMPConsentInformation.sharedInstance.canRequestAds
  15. }
  16. var isPrivacyOptionsRequired: Bool {
  17. return UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus == .required
  18. }
  19. /// 辅助方法,调用 UMP SDK 方法请求同意信息并加载/显示同意表单(如果需要)。
  20. func gatherConsent(
  21. from consentFormPresentationviewController: UIViewController,
  22. consentGatheringComplete: @escaping (Error?) -> Void
  23. ) {
  24. let parameters = UMPRequestParameters()
  25. // 用于测试目的,你可以强制设置 UMPDebugGeography 为 EEA 或非 EEA。
  26. let debugSettings = UMPDebugSettings()
  27. #if DEBUG
  28. debugSettings.geography = UMPDebugGeography.EEA
  29. #endif
  30. parameters.debugSettings = debugSettings
  31. // 应该在每次应用启动时调用请求同意信息的更新。
  32. UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters) {
  33. requestConsentError in
  34. guard requestConsentError == nil else {
  35. return consentGatheringComplete(requestConsentError)
  36. }
  37. UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) {
  38. loadAndPresentError in
  39. // 已获取同意。
  40. consentGatheringComplete(loadAndPresentError)
  41. }
  42. }
  43. }
  44. /// 辅助方法,调用 UMP SDK 方法来呈现隐私选项表单。
  45. func presentPrivacyOptionsForm(
  46. from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void
  47. ) {
  48. UMPConsentForm.presentPrivacyOptionsForm(
  49. from: viewController, completionHandler: completionHandler)
  50. }
  51. }