开始使用

根据 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