开始使用

根据 Google《欧盟地区用户意见征求政策》,您必须向位于欧洲经济区 (EEA) 和英国的用户披露某些信息,在法律有相应要求的情况下,并获得 Cookie 的使用同意,才能使用个人数据(如 AdID)来投放广告。此政策反映了欧盟《电子隐私指令》和《一般数据保护条例》(GDPR) 的要求。

为了帮助发布商履行此政策规定的职责,Google 提供了 User Messaging Platform (UMP) SDK。UMP SDK 现已更新,支持最新的 IAB 标准。所有这些配置现在都可以在 AdMob “隐私权和消息”中轻松处理。

前提条件

用户消息类型

如需查看完整的受支持消息列表,请参阅 用户消息类型 。如需了解有关实现每种消息类型的具体说明,请参阅左侧导航栏。

导入 SDK

CocoaPods(首选)

自 Google 移动广告 SDK 7.64.0 起,UMP SDK 将作为 Google 移动广告 SDK Pod 的依赖项包含在内。

如要将该 SDK 导入 iOS 项目,最简便的方法就是使用 CocoaPods。请打开项目的 Podfile 并将下面这行代码添加到应用的目标中:

pod 'Google-Mobile-Ads-SDK'

然后运行以下命令:

pod install --repo-update

如果您刚开始接触 CocoaPods,请参阅如何使用 CocoaPods,详细了解如何创建和使用 Podfile。

手动下载

另一种方法是手动导入 SDK。

下载 SDK

然后,将框架拖入您的 Xcode 项目中,确保已选中 Copy items if needed

然后便可以将该框架包含在需要使用的任何文件中:

Swift

import UserMessagingPlatform

Objective-C

#include <UserMessagingPlatform/UserMessagingPlatform.h>

更新您的 Info.plist

查找应用 ID 并将其添加到 Info.plist 中:

<key>GADApplicationIdentifier</key>
<string>ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy</string>

确定是否需要显示消息

每次启动应用时,您都应在加载表单之前使用 requestConsentInfoUpdateWithParameters:completionHandler: 请求更新用户的意见征求信息。这样可以确定您的用户是否需要提供用户意见征求结果(如果尚未提供或者用户意见征求结果已过期)。

如有需要,您可以稍后再向用户显示表单

在应用启动时检查状态的示例如下:

Swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
   // Create a UMPRequestParameters object.
   let parameters = UMPRequestParameters()
   // Set tag for under age of consent. Here false means users are not under age.
   parameters.tagForUnderAgeOfConsent = false

   // Request an update to the consent information.
   UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(
      with: parameters,
      completionHandler: { error in
        if error != nil {
           // Handle the error.
        } else {
           // The consent information state was updated.
           // You are now ready to check if a form is
           // available.
        }
       })
}

Objective-C

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

// 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.
                           }
                         }];
}

加载表单(如果可用)

在显示表单之前,您需要先确定表单是否可用。表单不可用的原因可能是用户启用了受限的广告跟踪,或者您已将这些用户标记为“未达到同意年龄”。

如需检查表单是否可用,请使用您之前创建的the formStatus property on the UMPConsentInformation instance 。

然后,添加一个加载表单所需的封装方法:

Swift

// Request an update to the consent information.
UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(
    withParameters: parameters,
    completionHandler: { [self] error in

      // The consent information has updated.
      if error != nil {
        // Handle the error.
      } else {
        // The consent information state was updated.
        // You are now ready to see if a form is available.
        let formStatus = UMPConsentInformation.sharedInstance.formStatus
        if formStatus == UMPFormStatus.available {
          loadForm()
        }
      }
    })
...
func loadForm() {

}

Objective-C

// Request an update to the consent information.
[UMPConsentInformation.sharedInstance
    requestConsentInfoUpdateWithParameters:parameters
                         completionHandler:^(NSError* _Nullable error) {

                           // The consent information has updated.
                           if (error) {
                             // Handle the error.
                           } else {
                             // The consent information state was updated.
                             // You are now ready to see if a form is available.
                             UMPFormStatus formStatus =
                                 UMPConsentInformation.sharedInstance
                                     .formStatus;
                             if (formStatus == UMPFormStatusAvailable) {
                               [self loadForm];
                             }
                           }
                         }];
...
- (void) loadForm {

}

如需加载表单,请使用 the static loadWithCompletionHandler: method on the UMPConsentForm class,

Swift

func loadForm() {
  // Loads a consent form. Must be called on the main thread.
  UMPConsentForm.load(
      withCompletionHandler: { form, loadError in
        if loadError != nil {
          // Handle the error
        } else {
          // Present the form
        }
      })
}

Objective-C

- (void)loadForm {
  [UMPConsentForm
      loadWithCompletionHandler:^(UMPConsentForm *form, NSError *loadError) {
        if (loadError) {
          // Handle the error
        } else {
          // Present the form
        }
      }];
}

根据需要显示表单

在确定表单是否可用并完成加载后,在UMPConsentForm 实例上通过presentFromViewController:completionHandler: 方法显示表单。

使用之前的UMPConsentInformation 对象检查consent status 并更新loadForm 方法:

Swift

func loadForm() {
  UMPConsentForm.load(withCompletionHandler: { form, loadError in
    if loadError != nil {
      // Handle the error.
    } else {
      // Present the form. You can also hold on to the reference to present
      // later.
      if UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatus.required {
        form?.present(
            from: self,
            completionHandler: { dismissError in
              if UMPConsentInformation.sharedInstance.consentStatus == UMPConsentStatus.obtained {
                // App can start requesting ads.
              }
                // Handle dismissal by reloading form.
                loadForm();
            })
      } else {
        // Keep the form available for changes to user consent.
      }
    }
  })
}

Objective-C

- (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.
                      }
                        // Handle dismissal by reloading form.
                        [self loadForm];
                    }];
      } else {
        // Keep the form available for changes to user consent.
      }
    }
  }];
}

如果您需要在用户做出选择或关闭表单后执行操作,请将相应逻辑放入表单的完成处理程序或回调中。

测试

强制调试地理位置

UMP SDK 提供了一种测试应用行为的方式,就像使用 the debugGeography property of type UMPDebugGeography on UMPDebugSettings的设备位于欧洲经济区 (EEA) 或英国内一样。

您必须在应用的调试设置中提供经过哈希处理的测试设备 ID 才能使用调试功能。如果您在未设置该值的情况下调用requestConsentInfoUpdateWithParameters:completionHandler: ,您的应用将在运行时记录经过哈希处理的所需 ID。

Swift

let parameters = UMPRequestParameters()
let debugSettings = UMPDebugSettings()
debugSettings.testDeviceIdentifiers = ["TEST-DEVICE-HASHED-ID"]
debugSettings.geography = UMPDebugGeography.EEA
parameters.debugSettings = debugSettings
UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(
    with: parameters,
    completionHandler: { error in
      ...
    })

Objective-C

UMPRequestParameters *parameters = [[UMPRequestParameters alloc] init];
UMPDebugSettings *debugSettings = [[UMPDebugSettings alloc] init];
debugSettings.testDeviceIdentifiers = @[ @"TEST-DEVICE-HASHED-ID" ];
debugSettings.geography = UMPDebugGeographyEEA;
parameters.debugSettings = debugSettings;
[UMPConsentInformation.sharedInstance
    requestConsentInfoUpdateWithParameters:parameters
                         completionHandler:^(NSError *_Nullable error){
                           ...
}];

借助 UMPDebugGeography,您可以选择将地理位置强制设置为以下选项之一:

调试地理位置 说明
UMPDebugGeographyDisabled 调试地理位置功能已停用。
UMPDebugGeographyEEA 对于调试设备,地理位置会显示为欧洲经济区 (EEA)。
UMPDebugGeographyNotEEA 对于调试设备,地理位置会显示为不在欧洲经济区 (EEA)。

请注意,调试设置仅适用于测试设备。无需将模拟器添加到您的设备 ID 列表中,因为模拟器已默认启用测试。

通过 UMP SDK 测试应用时,您可能会发现重置 SDK 的状态很实用,因为您可以模拟用户的首次安装体验。该 SDK 提供的 reset 方法可实现此目的。

Swift

UMPConsentInformation.sharedInstance.reset()

Objective-C

[UMPConsentInformation.sharedInstance reset];

如果您决定从项目中完全移除 UMP SDK,也应调用 reset