1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- //
- // GoogleMobileAdsConsentManager.swift
- // PhysicalWallPaper
- //
- // Created by nkl on 2024/12/16.
- //
- import Foundation
- import GoogleMobileAds
- import UserMessagingPlatform
- /// Google Mobile Ads SDK 提供了用户消息平台(Google 的 IAB 认证的同意管理平台)作为在受 GDPR 影响的国家获取用户同意的解决方案之一。这是一个示例,你可以选择另一个同意管理平台来获取同意。
- class GoogleMobileAdsConsentManager: NSObject {
- static let shared = GoogleMobileAdsConsentManager()
- var canRequestAds: Bool {
- return UMPConsentInformation.sharedInstance.canRequestAds
- }
- var isPrivacyOptionsRequired: Bool {
- return UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus == .required
- }
- /// 辅助方法,调用 UMP SDK 方法请求同意信息并加载/显示同意表单(如果需要)。
- func gatherConsent(
- from consentFormPresentationviewController: UIViewController,
- consentGatheringComplete: @escaping (Error?) -> Void
- ) {
- let parameters = UMPRequestParameters()
- // 用于测试目的,你可以强制设置 UMPDebugGeography 为 EEA 或非 EEA。
- let debugSettings = UMPDebugSettings()
- #if DEBUG
- debugSettings.geography = UMPDebugGeography.EEA
- #endif
- parameters.debugSettings = debugSettings
-
- // 应该在每次应用启动时调用请求同意信息的更新。
- UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters) {
- requestConsentError in
- guard requestConsentError == nil else {
- return consentGatheringComplete(requestConsentError)
- }
- UMPConsentForm.loadAndPresentIfRequired(from: consentFormPresentationviewController) {
- loadAndPresentError in
- // 已获取同意。
- consentGatheringComplete(loadAndPresentError)
- }
- }
- }
- /// 辅助方法,调用 UMP SDK 方法来呈现隐私选项表单。
- func presentPrivacyOptionsForm(
- from viewController: UIViewController, completionHandler: @escaping (Error?) -> Void
- ) {
- UMPConsentForm.presentPrivacyOptionsForm(
- from: viewController, completionHandler: completionHandler)
- }
- }
|