Bắt đầu

SDK Nền tảng thông báo cho người dùng (UMP) của Google là một công cụ về quyền riêng tư và thông báo để giúp bạn quản lý các lựa chọn về quyền riêng tư. Để biết thêm thông tin, hãy xem Giới thiệu về Quyền riêng tư & nhắn tin. Bạn có thể thấy quá trình triển khai IMA hoạt động với UMP SDK trong Mục tiêu C hoặc Swift Ứng dụng mẫu UMP.

Tạo loại thông báo

Tạo thông báo cho người dùng bằng một trong Các loại thông báo cho người dùng hiện có trong Chính sách quyền riêng tư & nhắn tin trong Trình quản lý Quảng cáo tài khoản. UMP SDK cố gắng hiển thị thông báo về quyền riêng tư được tạo từ Interactive Media Ads Mã ứng dụng đã thiết lập trong dự án của bạn.

Để biết thêm thông tin, hãy xem Giới thiệu về quyền riêng tư và thông báo.

Nhập SDK

SDK UMP không có trong phần phụ thuộc của SDK IMA, vì vậy, bạn phải hãy tự thêm nó một cách rõ ràng.

CocoaPods (ưu tiên)

Cách dễ nhất để nhập SDK vào một dự án iOS là sử dụng CocoaPods Mở tệp Podfile và thêm dòng này vào mục tiêu của ứng dụng:

pod 'GoogleUserMessagingPlatform'

Sau đó, chạy lệnh sau:

pod install --repo-update

Nếu bạn mới sử dụng CocoaPods, hãy xem phần Sử dụng CocoaPods để biết thông tin chi tiết về cách tạo và sử dụng Podfile.

Trình quản lý gói Swift

SDK UMP cũng hỗ trợ Trình quản lý gói Swift. Hãy làm theo các bước sau để nhập gói Swift.

  1. Trong Xcode, hãy cài đặt Gói Swift SDK UMP bằng cách chuyển đến Tệp > Thêm gói....

  2. Khi lời nhắc xuất hiện, hãy tìm gói Swift SDK UMP trên GitHub kho lưu trữ:

    https://github.com/googleads/swift-package-manager-google-user-messaging-platform.git
    
  3. Chọn phiên bản Gói Swift SDK UMP mà bạn muốn sử dụng. Cho người mới dự án của mình, bạn nên sử dụng phiên bản Up to Next Major Version (Lên phiên bản lớn tiếp theo).

Sau đó, Xcode giải quyết các phần phụ thuộc của gói và tải chúng xuống nền. Để biết thêm thông tin chi tiết về cách thêm các phần phụ thuộc của gói, hãy xem bài viết.

Thêm mã ứng dụng

Bạn có thể tìm thấy mã ứng dụng của mình trong Giao diện người dùng Ad Manager. Thêm mã nhận dạng vào Info.plist bằng đoạn mã sau:

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

Bạn nên yêu cầu cập nhật thông tin về sự đồng ý của người dùng ở mọi ứng dụng chạy, bằng requestConsentInfoUpdateWithParameters:completionHandler:. Yêu cầu này kiểm tra như sau:

  • Liệu sự đồng ý có bắt buộc hay không. Ví dụ: cần có sự đồng ý đối với lần đầu tiên hoặc quyết định đồng ý trước đó đã hết hạn.
  • Liệu có bắt buộc phải có điểm truy cập các tuỳ chọn quyền riêng tư hay không. Một số thông báo về quyền riêng tư yêu cầu ứng dụng cho phép người dùng sửa đổi tuỳ chọn quyền riêng tư của họ bất cứ lúc nào.

Dưới đây là ví dụ về cách kiểm tra trạng thái từ UIViewController trong 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 privacy message 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 privacy message form.
          }];
}

Tải và trình bày biểu mẫu thông báo về quyền riêng tư (nếu cần)

Sau khi bạn nhận được trạng thái đồng ý mới nhất, hãy gọi loadAndPresentIfRequiredFromViewController:completionHandler: để tải mọi biểu mẫu cần thiết để thu thập sự đồng ý của người dùng. Sau khi tải, các biểu mẫu sẽ xuất hiện ngay lập tức.

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

Nếu bạn cần thực hiện bất kỳ hành động nào sau khi người dùng đã đưa ra lựa chọn hoặc bỏ qua biểu mẫu, hãy đặt logic đó vào completion handler cho biểu mẫu của mình.

Tuỳ chọn quyền riêng tư

Một số biểu mẫu thông báo về quyền riêng tư được trình bày dựa trên quyền riêng tư do nhà xuất bản hiển thị điểm truy cập, cho phép người dùng quản lý các tuỳ chọn về quyền riêng tư của họ bất cứ lúc nào. Để tìm hiểu thêm về loại thông báo mà người dùng của bạn nhìn thấy trong phần tuỳ chọn quyền riêng tư điểm vào, xem Các loại thông báo cho người dùng hiện có.

Để triển khai điểm truy cập các lựa chọn về quyền riêng tư, hãy hoàn tất các bước sau:

  1. Hãy xem UMPConsentInformation.sharedInstance.privacyOptionsRequirementStatus.
  2. Nếu điểm truy cập các tuỳ chọn quyền riêng tư là là bắt buộc, hãy thêm thành phần giao diện người dùng có thể nhìn thấy và tương tác vào ứng dụng của bạn.
  3. Kích hoạt biểu mẫu về các lựa chọn về quyền riêng tư bằng cách sử dụng presentPrivacyOptionsFormFromViewController:completionHandler:.

Ví dụ về mã sau đây minh hoạ các bước này:

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

Yêu cầu quảng cáo

Trước khi yêu cầu quảng cáo trong ứng dụng, hãy kiểm tra xem bạn đã nhận được sự đồng ý hay chưa từ người dùng đang sử dụng UMPConsentInformation.sharedInstance.canRequestAds. Có hai các địa điểm cần kiểm tra trong quá trình thu thập sự đồng ý:

  • Sau khi thu thập được sự đồng ý trong phiên hiện tại.
  • Ngay sau khi bạn gọi requestConsentInfoUpdateWithParameters:completionHandler:. Có thể bạn đã nhận được sự đồng ý trong buổi chia sẻ trước. Dưới dạng độ trễ phương pháp hay nhất, chúng tôi khuyên bạn không nên đợi lệnh gọi lại hoàn tất để bạn có thể hãy bắt đầu tải quảng cáo ngay sau khi ứng dụng của bạn khởi chạy.

Nếu xảy ra lỗi trong quá trình thu thập sự đồng ý, bạn vẫn nên cố yêu cầu quảng cáo. UMP SDK sử dụng trạng thái đồng ý từ trước phiên hoạt động.

Swift

class ViewController: UIViewController {

  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.startImaSdk()
        }
      }
    }
    
    // Check if you can initialize the IMA 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 {
      startImaSdk()
    }
  }
  
  private func startImaSdk() {
    // Create an IMAAdsLoader instance.
    adsLoader = IMAAdsLoader(settings: nil)

    // TODO: Create an IMAAdDisplayContainer and IMAAdsRequest, then make
    // a request for ads.
  }
}

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 startImaSdk];
                  }
                }];
          }];

  // Check if you can initialize the Google IMA 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 startImaSdk];
  }
}

- (void)startImaSdk {
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    // Create an IMAAdsLoader instance.
    self.adsLoader = [[IMAAdsLoader alloc] init];

    // TODO: Create an IMAAdDisplayContainer and IMAAdsRequest, then make
    // a request for ads.
  });
}

Thử nghiệm

Nếu bạn muốn kiểm thử việc tích hợp trong ứng dụng khi bạn đang phát triển, hãy làm theo các bước sau để đăng ký thiết bị thử nghiệm của bạn theo phương thức lập trình. Hãy nhớ gỡ bỏ để đặt các mã thiết bị thử nghiệm này trước khi bạn phát hành ứng dụng của mình.

  1. Gọi requestConsentInfoUpdateWithParameters:completionHandler:.
  2. Kiểm tra đầu ra nhật ký để tìm một thông báo tương tự như ví dụ sau: cho biết mã thiết bị của bạn và cách thêm thiết bị đó làm thiết bị thử nghiệm:

    <UMP SDK>To enable debug mode for this device, set: UMPDebugSettings.testDeviceIdentifiers = @[2077ef9a63d2b398840261c8221a0c9b]
    
  3. Sao chép mã thiết bị thử nghiệm vào bảng nhớ tạm.

  4. Sửa đổi mã của bạn để gọi UMPDebugSettings().testDeviceIdentifiers rồi truyền vào danh sách mã thiết bị thử nghiệm của bạn.

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

Buộc hiển thị một khu vực địa lý

UMP SDK cung cấp một cách để thử nghiệm hành vi của ứng dụng như thể thiết bị ở Khu vực kinh tế Châu Âu (EEA) hoặc Vương quốc Anh bằng the debugGeography property of type UMPDebugGeography on UMPDebugSettings. Lưu ý rằng chế độ cài đặt gỡ lỗi chỉ hoạt động trên thiết bị thử nghiệm.

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

Khi thử nghiệm ứng dụng với UMP SDK, bạn có thể thấy hữu ích khi đặt lại trạng thái của SDK để bạn có thể mô phỏng trải nghiệm cài đặt lần đầu của người dùng. SDK cung cấp phương thức reset để thực hiện việc này.

Swift

UMPConsentInformation.sharedInstance.reset()

Objective-C

[UMPConsentInformation.sharedInstance reset];

Ví dụ trên GitHub

Ví dụ về việc tích hợp SDK Nền tảng thông báo cho người dùng (UMP SDK): |