分享您的意見,協助我們打造 Google Mobile Ads SDK 的藍圖。請在 2023 年 5 月 5 日以前完成 Google Mobile Ads SDK 2023 年年度問卷調查

開始使用

透過集合功能整理內容 你可以依據偏好儲存及分類內容。

根據 Google 的《歐盟地區使用者同意授權政策》規定,您必須向英國和歐洲經濟區境內的使用者揭露特定資訊。另外,在法律要求的情況下,您必須取得使用者同意才能使用 Cookie 或其他本機儲存空間,並且使用個人資料 (例如 AdID) 放送廣告。本政策是配合《歐盟地區電子通訊隱私指令》和《一般資料保護規則》(GDPR) 而製定。

為了協助發布者根據這項政策履行自身職責,Google 提供了 User Messaging Platform (UMP) SDK。UMP SDK 已更新為支援最新的 IAB 標準以上所有設定現在都能輕鬆用於 AdMob 隱私權與訊息。

必要條件

使用者訊息類型

如需支援訊息的完整清單,請參閱 使用者訊息類型 如需實作各種訊息類型的特定操作說明,請參閱左側導覽列。

匯入 SDK

CocoaPods (偏好)

UMP SDK 包含在 Google Mobile Ads SDK 7.64.0 之前的 Google Mobile Ads SDK Pod 依附元件。

將 SDK 匯入 iOS 專案最簡單的方法就是使用 CocoaPods。開啟專案的 Podfile,然後將這一行新增至應用程式的目標:

pod 'Google-Mobile-Ads-SDK'

然後執行下列指令:

pod install --repo-update

如果您是第一次使用 CocoaPods,請參閱使用 CocoaPods 以進一步瞭解如何建立及使用 Podfiles。

手動下載

匯入 SDK 的另一種方法,就是手動匯入。

下載 SDK

接著,將架構拖曳至您的 Xcode 專案,確認您已選取 [視需要複製項目]

然後,您可以在任何所需的檔案中將架構加入:

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一樣。

您必須在應用程式的偵錯設定中提供測試裝置的雜湊 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 偵錯裝置的顯示位置為歐洲經濟區。
UMPDebugGeographyNotEEA 針對偵錯裝置,地理位置並非位於歐洲經濟區。

請注意,偵錯設定僅適用於測試裝置。根據預設,模擬器已在裝置 ID 清單中加入,因為他們已啟用測試功能。

使用 UMP SDK 測試應用程式時,建議您重設 SDK 的狀態,以便模擬使用者初次安裝體驗。SDK 會提供 reset 方法,

Swift

UMPConsentInformation.sharedInstance.reset()

Objective-C

[UMPConsentInformation.sharedInstance reset];

如果您決定全面移除 UMP SDK,則也應呼叫 reset