เริ่มต้น

ภายใต้นโยบายความยินยอมของผู้ใช้ EU ของ Google คุณต้องเปิดเผยข้อมูลบางอย่างต่อผู้ใช้ในเขตเศรษฐกิจยุโรป (EEA) รวมถึงสหราชอาณาจักร และขอความยินยอมในการใช้คุกกี้หรือพื้นที่เก็บข้อมูลอื่นในเครื่องในกรณีที่กฎหมายกำหนด และใช้ข้อมูลส่วนตัว (เช่น AdID) เพื่อแสดงโฆษณา นโยบายนี้เป็นผลมาจากข้อกำหนดด้าน ePrivacy และกฎระเบียบให้ความคุ้มครองข้อมูลส่วนบุคคลของผู้บริโภค (GDPR) ของสหภาพยุโรป

Google มี SDK สำหรับ User Messaging Platform (UMP) เพื่อสนับสนุนให้ผู้เผยแพร่โฆษณาปฏิบัติตามหน้าที่ของตนภายใต้นโยบายนี้ UMP SDK ได้รับการอัปเดตให้รองรับ มาตรฐาน IAB ล่าสุด ตอนนี้การกำหนดค่าทั้งหมดนี้จัดการได้ง่ายๆ ใน AdMob ความเป็นส่วนตัวและการแสดงข้อความแจ้งผู้ใช้

ข้อกำหนดเบื้องต้น

สร้างประเภทข้อความ

สร้างข้อความสำหรับผู้ใช้โดยใช้ ประเภทข้อความสำหรับผู้ใช้ที่มีให้บริการ ในแท็บความเป็นส่วนตัวและการรับส่งข้อความของบัญชี AdMob ของคุณ UMP SDK จะพยายามแสดงข้อความสำหรับผู้ใช้ที่สร้างขึ้นจาก AdMob รหัสแอปพลิเคชันที่ตั้งค่าไว้ในโปรเจ็กต์ หากไม่มีการกำหนดค่าข้อความไว้สำหรับแอปพลิเคชันของคุณ SDK จะแสดงผลข้อผิดพลาด

ดูรายละเอียดเพิ่มเติมได้ที่ เกี่ยวกับความเป็นส่วนตัวและการรับส่งข้อความ

นำเข้า SDK

CocoaPods (แนะนำ)

วิธีที่ง่ายที่สุดในการนำเข้า SDK ไปยังโปรเจ็กต์ iOS คือการใช้ CocoaPods เปิด Podfile ของโปรเจ็กต์ แล้วเพิ่มบรรทัดนี้ลงในเป้าหมายของแอป

pod 'GoogleUserMessagingPlatform'

จากนั้นเรียกใช้คำสั่งต่อไปนี้

pod install --repo-update

หากคุณเพิ่งเคยใช้ CocoaPods โปรดดูรายละเอียดเกี่ยวกับวิธีสร้างและใช้ Podfile ที่การใช้ CocoaPods

ผู้จัดการแพ็กเกจของ Swift

UMP SDK ยังรองรับ Swift Package Manager ด้วย ทำตามขั้นตอนเหล่านี้เพื่อ นำเข้าแพ็กเกจ Swift

  1. ใน Xcode ให้ติดตั้ง UMP SDK Swift Package โดยไปที่ File > Add Packages...

  2. ในข้อความแจ้งที่ปรากฏขึ้น ให้ค้นหาที่เก็บ UMP SDK Swift Package GitHub:

    https://github.com/googleads/swift-package-manager-google-user-messaging-platform.git
    
  3. เลือกเวอร์ชันของ UMP SDK Swift Package ที่ต้องการใช้ สำหรับโปรเจ็กต์ใหม่ เราขอแนะนำให้ใช้เวอร์ชันหลักถัดไป

จากนั้น Xcode จะแก้ไขทรัพยากร Dependency ของแพ็กเกจ และดาวน์โหลดรายการดังกล่าวในเบื้องหลัง ดูรายละเอียดเพิ่มเติมเกี่ยวกับวิธีเพิ่มทรัพยากร Dependency ของแพ็กเกจได้ที่บทความของ Apple

ดาวน์โหลดด้วยตนเอง

อีกวิธีหนึ่งในการนำเข้า SDK คือการดำเนินการด้วยตนเอง

ดาวน์โหลด SDK

จากนั้นลากเฟรมเวิร์กลงในโปรเจ็กต์ Xcode แล้วเลือกคัดลอกรายการหากจำเป็น

จากนั้นคุณจะรวมเฟรมเวิร์กไว้ในไฟล์ใดก็ได้ที่ต้องการโดยใช้วิธีต่อไปนี้

Swift

import UserMessagingPlatform

Objective-C

#include <UserMessagingPlatform/UserMessagingPlatform.h>

คุณควรส่งคำขออัปเดตข้อมูลความยินยอมของผู้ใช้ทุกครั้งที่เปิดแอปโดยใช้ requestConsentInfoUpdateWithParameters:completionHandler:การดำเนินการนี้จะกำหนดว่าผู้ใช้ต้องให้ความยินยอมในกรณีที่ยังไม่ได้ดำเนินการ หรือความยินยอมหมดอายุแล้ว

ต่อไปนี้คือตัวอย่างวิธีตรวจสอบสถานะจาก UIViewController ในเมธอด viewDidLoad()

Swift

override func viewDidLoad() {
  super.viewDidLoad()

  // Request an update for the consent information.
  UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: nil) {
    [weak self] requestConsentError in
    guard let self else { return }

    if let consentError = requestConsentError {
      // Consent gathering failed.
      return print("Error: \(consentError.localizedDescription)")
    }

    // TODO: Load and present the consent form.
  }
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  // Request an update for the consent information.
  [UMPConsentInformation.sharedInstance
      requestConsentInfoUpdateWithParameters:nil
          completionHandler:^(NSError *_Nullable requestConsentError) {
            if (requestConsentError) {
              // Consent gathering failed.
              NSLog(@"Error: %@", requestConsentError.localizedDescription);
              return;
            }

            // TODO: Load and present the consent form.
          }];
}

โหลดและแสดงแบบฟอร์มความยินยอมหากจำเป็น

สำคัญ: API ต่อไปนี้ใช้งานได้กับ UMP SDK เวอร์ชัน 2.1.0 ขึ้นไป

หลังจากได้รับสถานะความยินยอมเวอร์ชันล่าสุดแล้ว โปรดโทรหาloadAndPresentIfRequiredFromViewController:completionHandler: ในชั้นเรียนUMPConsentForm เพื่อโหลดแบบฟอร์มความยินยอม หากต้องมีสถานะความยินยอม SDK จะโหลดแบบฟอร์มและแสดงทันที จาก view controllerที่ระบุ ระบบจะ completion handler ให้เสร็จสมบูรณ์ หลังจากที่ปิดแบบฟอร์ม หากไม่จำเป็นต้องให้ความยินยอม ระบบจะเรียกใช้ completion handler ทันที

Swift

override func viewDidLoad() {
  super.viewDidLoad()

  // Request an update for the consent information.
  UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: nil) {
    [weak self] requestConsentError in
    guard let self else { return }

    if let consentError = requestConsentError {
      // Consent gathering failed.
      return print("Error: \(consentError.localizedDescription)")
    }

    UMPConsentForm.loadAndPresentIfRequired(from: self) {
      [weak self] loadAndPresentError in
      guard let self else { return }

      if let consentError = loadAndPresentError {
        // Consent gathering failed.
        return print("Error: \(consentError.localizedDescription)")
      }

      // Consent has been gathered.
    }
  }
}

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  __weak __typeof__(self) weakSelf = self;
  // Request an update for the consent information.
  [UMPConsentInformation.sharedInstance
      requestConsentInfoUpdateWithParameters:nil
          completionHandler:^(NSError *_Nullable requestConsentError) {
            if (requestConsentError) {
              // Consent gathering failed.
              NSLog(@"Error: %@", requestConsentError.localizedDescription);
              return;
            }

            __strong __typeof__(self) strongSelf = weakSelf;
            if (!strongSelf) {
              return;
            }

            [UMPConsentForm loadAndPresentIfRequiredFromViewController:strongSelf
                completionHandler:^(NSError *loadAndPresentError) {
                  if (loadAndPresentError) {
                    // Consent gathering failed.
                    NSLog(@"Error: %@", loadAndPresentError.localizedDescription);
                    return;
                  }

                  // Consent has been gathered.
                }];
          }];
}

หากต้องการดำเนินการใดๆ หลังจากที่ผู้ใช้เลือกหรือปิดแบบฟอร์มแล้ว ให้วางตรรกะนั้นไว้ใน completion handler ของแบบฟอร์ม

ส่งคำขอแสดงโฆษณา

ก่อนที่จะขอโฆษณาในแอป โปรดตรวจสอบว่าคุณได้รับความยินยอม จากผู้ใช้ที่ใช้ UMPConsentInformation.sharedInstance.canRequestAdsหรือไม่ ขณะที่รวบรวมความยินยอม คุณจะมี 2 ส่วนดังนี้

  1. เมื่อรวบรวมความยินยอมในเซสชันปัจจุบันแล้ว
  2. ทันทีที่คุณโทรหา requestConsentInfoUpdateWithParameters:completionHandler: เป็นไปได้ว่าได้รับความยินยอมแล้วในเซสชันก่อนหน้า สำหรับแนวทางปฏิบัติแนะนำสำหรับเวลาในการตอบสนอง เราไม่แนะนำให้รอการโทรกลับเสร็จสมบูรณ์ เพื่อที่คุณจะได้เริ่มโหลดโฆษณาโดยเร็วที่สุดหลังจากเปิดตัวแอปแล้ว

หากเกิดข้อผิดพลาดระหว่างกระบวนการรวบรวมความยินยอม คุณควรพยายามขอโฆษณา UMP SDK ใช้สถานะความยินยอมจากเซสชันก่อนหน้า

Swift

class ViewController: UIViewController {

  // Use a boolean to initialize the Google Mobile Ads SDK and load ads once.
  private var isMobileAdsStartCalled = false

  override func viewDidLoad() {
    super.viewDidLoad()

    // Request an update for the consent information.
    UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: nil) {
      [weak self] requestConsentError in
      guard let self else { return }

      if let consentError = requestConsentError {
        // Consent gathering failed.
        return print("Error: \(consentError.localizedDescription)")
      }

      UMPConsentForm.loadAndPresentIfRequired(from: self) {
        [weak self] loadAndPresentError in
        guard let self else { return }

        if let consentError = loadAndPresentError {
          // Consent gathering failed.
          return print("Error: \(consentError.localizedDescription)")
        }

        // Consent has been gathered.
        if UMPConsentInformation.sharedInstance.canRequestAds {
          self.startGoogleMobileAdsSDK()
        }
      }
    }
    
    // Check if you can initialize the Google Mobile Ads SDK in parallel
    // while checking for new consent information. Consent obtained in
    // the previous session can be used to request ads.
    if UMPConsentInformation.sharedInstance.canRequestAds {
      startGoogleMobileAdsSDK()
    }
  }
  
  private func startGoogleMobileAdsSDK() {
    DispatchQueue.main.async {
      guard !self.isMobileAdsStartCalled else { return }

      self.isMobileAdsStartCalled = true

      // Initialize the Google Mobile Ads SDK.
      GADMobileAds.sharedInstance().start()

      // TODO: Request an ad.
      // GADInterstitialAd.load(...)
    }
  }
}

Objective-C

@implementation ViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  __weak __typeof__(self) weakSelf = self;
  // Request an update for the consent information.
  [UMPConsentInformation.sharedInstance
      requestConsentInfoUpdateWithParameters:nil
          completionHandler:^(NSError *_Nullable requestConsentError) {
            if (requestConsentError) {
              // Consent gathering failed.
              NSLog(@"Error: %@", requestConsentError.localizedDescription);
              return;
            }
            __strong __typeof__(self) strongSelf = weakSelf;
            if (!strongSelf) {
              return;
            }

            [UMPConsentForm loadAndPresentIfRequiredFromViewController:strongSelf
                completionHandler:^(NSError *loadAndPresentError) {
                  if (loadAndPresentError) {
                    // Consent gathering failed.
                    NSLog(@"Error: %@", loadAndPresentError.localizedDescription);
                    return;
                  }

                  // Consent has been gathered.
                  __strong __typeof__(self) strongSelf = weakSelf;
                  if (!strongSelf) {
                    return;
                  }

                  if (UMPConsentInformation.sharedInstance.canRequestAds) {
                    [strongSelf startGoogleMobileAdsSDK];
                  }
                }];
          }];

  // Check if you can initialize the Google Mobile Ads SDK in parallel
  // while checking for new consent information. Consent obtained in
  // the previous session can be used to request ads.
  if (UMPConsentInformation.sharedInstance.canRequestAds) {
    [self startGoogleMobileAdsSDK];
  }
}

- (void)startGoogleMobileAdsSDK {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    // Initialize the Google Mobile Ads SDK.
    [GADMobileAds.sharedInstance startWithCompletionHandler:nil];

    // TODO: Request an ad.
    // [GADInterstitialAd loadWithAdUnitID...];
  });
}

ตัวเลือกความเป็นส่วนตัว

แบบฟอร์มความยินยอมบางแบบฟอร์มกำหนดให้ผู้ใช้แก้ไขความยินยอมได้ทุกเมื่อ ทำตามขั้นตอนต่อไปนี้เพื่อใช้ปุ่มตัวเลือกความเป็นส่วนตัว หากจำเป็น

ทั้งสองสามารถทำได้โดยดำเนินการดังนี้

  1. ใช้องค์ประกอบ UI เช่น ปุ่มในหน้าการตั้งค่าของแอปที่สามารถทริกเกอร์แบบฟอร์มตัวเลือกความเป็นส่วนตัว
  2. เมื่อ loadAndPresentIfRequiredFromViewController:completionHandler: เสร็จสิ้นแล้ว ให้เลือกprivacyOptionsRequirementStatus เพื่อระบุว่าจะแสดงองค์ประกอบ UI ที่แสดงแบบฟอร์มตัวเลือกความเป็นส่วนตัวได้หรือไม่
  3. เมื่อผู้ใช้โต้ตอบกับองค์ประกอบ UI ให้เรียกใช้presentPrivacyOptionsFormFromViewController:completionHandler: เพื่อแสดงแบบฟอร์มเพื่อให้ผู้ใช้อัปเดตตัวเลือกความเป็นส่วนตัวได้ทุกเมื่อ

ตัวอย่างต่อไปนี้แสดงวิธีนำเสนอแบบฟอร์มตัวเลือกความเป็นส่วนตัวจาก UIBarButtonItem

Swift

@IBOutlet weak var privacySettingsButton: UIBarButtonItem!

var isPrivacyOptionsRequired: Bool {
  return UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus == .required
}

override func viewDidLoad() {
  // ...

  // Request an update for the consent information.
  UMPConsentInformation.sharedInstance.requestConsentInfoUpdate(with: parameters) {
    // ...

    UMPConsentForm.loadAndPresentIfRequired(from: self) {
      //...

      // Consent has been gathered.

      // Show the button if privacy options are required.
      self.privacySettingsButton.isEnabled = isPrivacyOptionsRequired
    }
  }
  // ...
}

// Present the privacy options form when a user interacts with the
// privacy settings button.
@IBAction func privacySettingsTapped(_ sender: UIBarButtonItem) {
  UMPConsentForm.presentPrivacyOptionsForm(from: self) {
    [weak self] formError in
    guard let self, let formError else { return }

    // Handle the error.
  }
}

Objective-C

@interface ViewController ()
@property(weak, nonatomic) IBOutlet UIBarButtonItem *privacySettingsButton;
@end

- (BOOL)isPrivacyOptionsRequired {
  return UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus ==
         UMPPrivacyOptionsRequirementStatusRequired;
}

- (void)viewDidLoad {
  // ...

  __weak __typeof__(self) weakSelf = self;
  // Request an update for the consent information.
  [UMPConsentInformation.sharedInstance
      requestConsentInfoUpdateWithParameters:parameters
          completionHandler:^(NSError *_Nullable requestConsentError) {
            // ...

            [UMPConsentForm loadAndPresentIfRequiredFromViewController:strongSelf
                completionHandler:^(NSError *loadAndPresentError) {
                  // ...

                  // Consent has been gathered.

                  // Show the button if privacy options are required.
                  strongSelf.privacySettingsButton.enabled = isPrivacyOptionsRequired;
                }];
          }];
}

// Present the privacy options form when a user interacts with your
// privacy settings button.
- (IBAction)privacySettingsTapped:(UIBarButtonItem *)sender {
  [UMPConsentForm presentPrivacyOptionsFormFromViewController:self
                                completionHandler:^(NSError *_Nullable formError) {
                                  if (formError) {
                                    // Handle the error.
                                  }
                                }];
}

การทดสอบ

หากต้องการทดสอบการผสานรวมในแอประหว่างการพัฒนา โปรดทำตามขั้นตอนเหล่านี้เพื่อลงทะเบียนอุปกรณ์ทดสอบแบบเป็นโปรแกรม อย่าลืมนำโค้ดที่กำหนดรหัสอุปกรณ์ทดสอบเหล่านี้ออกก่อนที่จะเผยแพร่แอป

  1. โทร requestConsentInfoUpdateWithParameters:completionHandler:
  2. ตรวจสอบเอาต์พุตบันทึกเพื่อหาข้อความที่คล้ายกับตัวอย่างต่อไปนี้ ซึ่งจะแสดงรหัสอุปกรณ์และวิธีเพิ่มเป็นอุปกรณ์ทดสอบ

    <UMP SDK>To enable debug mode for this device, set: UMPDebugSettings.testDeviceIdentifiers = @[2077ef9a63d2b398840261c8221a0c9b]
    
  3. คัดลอกรหัสอุปกรณ์ทดสอบไปยังคลิปบอร์ด

  4. แก้ไขโค้ดเพื่อ เรียกใช้ UMPDebugSettings().testDeviceIdentifiers และส่ง รายการรหัสอุปกรณ์ทดสอบ

    Swift

    let parameters = UMPRequestParameters()
    let debugSettings = UMPDebugSettings()
    debugSettings.testDeviceIdentifiers = ["TEST-DEVICE-HASHED-ID"]
    parameters.debugSettings = debugSettings
    // Include the UMPRequestParameters in your consent request.
    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" ];
    parameters.debugSettings = debugSettings;
    // Include the UMPRequestParameters in your consent request.
    [UMPConsentInformation.sharedInstance
        requestConsentInfoUpdateWithParameters:parameters
                            completionHandler:^(NSError *_Nullable error){
                              ...
    }];
    

บังคับใช้ภูมิศาสตร์

UMP SDK มีวิธีทดสอบลักษณะการทำงานของแอปเสมือนว่าอุปกรณ์อยู่ใน EEA หรือสหราชอาณาจักรโดยใช้ the debugGeography property of type UMPDebugGeography on UMPDebugSettingsโปรดทราบว่าการตั้งค่าการแก้ไขข้อบกพร่องจะใช้งานได้ในอุปกรณ์ทดสอบเท่านั้น

Swift

let parameters = UMPRequestParameters()
let debugSettings = UMPDebugSettings()
debugSettings.testDeviceIdentifiers = ["TEST-DEVICE-HASHED-ID"]
debugSettings.geography = .EEA
parameters.debugSettings = debugSettings
// Include the UMPRequestParameters in your consent request.
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;
// Include the UMPRequestParameters in your consent request.
[UMPConsentInformation.sharedInstance
    requestConsentInfoUpdateWithParameters:parameters
                         completionHandler:^(NSError *_Nullable error){
                           ...
}];

ในการทดสอบแอปด้วย UMP SDK การรีเซ็ตสถานะของ SDK จะช่วยให้สามารถจำลองประสบการณ์การติดตั้งครั้งแรกของผู้ใช้ได้ SDK ให้ reset วิธีการในการดำเนินการ

Swift

UMPConsentInformation.sharedInstance.reset()

Objective-C

[UMPConsentInformation.sharedInstance reset];

ตัวอย่างใน GitHub

ตัวอย่างการผสานรวม UMP SDK Swift | Objective-C