Triển khai quảng cáo trong chế độ hình trong hình (thử nghiệm)

Chọn nền tảng: Android iOS

Hình trong hình

Quảng cáo hình trong hình (PiP) xuất hiện trong một cửa sổ nổi nằm phía trên nội dung trên màn hình, chẳng hạn như bài viết, nguồn cấp dữ liệu hoặc nội dung chơi trò chơi. Định dạng này cho phép người dùng tương tác với ứng dụng của bạn trong khi quảng cáo vẫn hiển thị. Để hiển thị quảng cáo không chiếm toàn bộ màn hình, hãy chọn định dạng này. Tìm hiểu thêm về quảng cáo trong chế độ hình trong hình.

Hướng dẫn này trình bày cách yêu cầu và hiển thị quảng cáo ở chế độ hình trong hình trong ứng dụng của bạn bằng Google Mobile Ads SDK.

Trước khi bắt đầu

Trước khi tiếp tục, hãy làm như sau:

Tải một quảng cáo

Để tải một đối tượng GADPictureInPictureAd, hãy tạo một yêu cầu quảng cáo và gọi phương thức load:

Swift

private func loadPictureInPictureAd() async {
  do {
    // Capture the PictureInPictureAd reference for later use.
    pipAd = try await PictureInPictureAd.load(
      with: adUnitID, request: Request())
    // Set the delegate to be notified of ad events.
    pipAd?.delegate = self
  } catch {
    print(
      "Picture-in-Picture ad failed to load: \(error.localizedDescription)")
  }
}

Thay thế adUnitID bằng mã đơn vị quảng cáo của bạn.

Objective-C

- (void)loadPictureInPictureAd {
  GADRequest *request = [GADRequest request];

  [GADPictureInPictureAd
       loadWithAdUnitID:kAdUnitID
                request:request
      completionHandler:^(GADPictureInPictureAd *_Nullable ad, NSError *_Nullable error) {
        if (error) {
          NSLog(@"Picture-in-Picture ad failed to load: %@", error);
          return;
        }
        // Capture the PictureInPictureAd reference for later use.
        self.pipAd = ad;
        // Set the delegate to be notified of ad events.
        self.pipAd.delegate = self;
      }];
}

Thay thế kAdUnitID bằng mã đơn vị quảng cáo của bạn.

Hiển thị quảng cáo

Để hiển thị quảng cáo ở chế độ hình trong hình trên màn hình, hãy định cấu hình các lựa chọn về chế độ hình trong hình và gọi phương thức show. Ví dụ sau đây đặt vị trí mặc định của quảng cáo và phạm vi trình bày thành màn hình:

Swift

private func showPictureInPictureAd() {
  // Use the loaded PictureInPictureAd instance.
  guard let pipAd else {
    print("No ad to show.")
    return
  }
  let options = PictureInPictureAdOptions()
  // Uses the Google Mobile Ads SDK's default screen position.
  options.position = .default
  // Binds the ad lifecycle to the host screen.
  options.presentationScope = .screen

  pipAd.show(with: options)
}

Objective-C

- (void)showPictureInPictureAd {
  // Use the loaded PictureInPictureAd instance.
  if (!self.pipAd) {
    NSLog(@"No ad to show.");
    return;
  }
  GADPictureInPictureAdOptions *options =
      [[GADPictureInPictureAdOptions alloc] init];
  // Uses the Google Mobile Ads SDK's default screen position.
  options.position = GADPictureInPictureAdPositionDefault;
  // Binds the ad lifecycle to the host screen.
  options.presentationScope = GADPictureInPictureAdPresentationScopeScreen;

  [self.pipAd showWithOptions:options];
}

Đặt vị trí

Theo mặc định, Google Mobile Ads SDK sẽ hiển thị quảng cáo ở chế độ hình trong hình ở góc dưới cùng bên phải màn hình khi quảng cáo xuất hiện lần đầu hoặc ở vị trí đã biết gần đây nhất nếu quảng cáo đã xuất hiện trước đó. Để tuỳ chỉnh vị trí xuất hiện của quảng cáo, hãy đặt vị trí trong chế độ hình trong hình. Ví dụ sau đây đặt vị trí ở trên cùng của nội dung ở góc trên cùng bên trái màn hình:

Swift

private func createTopLeftPositionOptions() -> PictureInPictureAdOptions {
  let options = PictureInPictureAdOptions()
  // Sets the ad position to the top-left corner of the screen.
  options.position = .topLeft
  return options
}

Objective-C

- (GADPictureInPictureAdOptions *)createTopLeftPositionOptions {
  GADPictureInPictureAdOptions *options =
      [[GADPictureInPictureAdOptions alloc] init];
  // Sets the ad position to the top-left corner of the screen.
  options.position = GADPictureInPictureAdPositionTopLeft;
  return options;
}

Để biết tất cả các vị trí có sẵn, hãy xem GADPictureInPictureAdPosition.

Đặt phạm vi trình bày

Theo mặc định, Google Mobile Ads SDK sẽ liên kết quảng cáo trong chế độ hình trong hình với màn hình máy chủ hiện tại. Google Mobile Ads SDK đóng quảng cáo khi hệ phân cấp view của màn hình lưu trữ không còn trong bộ nhớ. Để quảng cáo vẫn hiển thị sau khi màn hình lưu trữ bị xoá khỏi bộ nhớ, hãy đặt phạm vi trình bày thành ứng dụng:

Swift

private func createApplicationScopedOptions() -> PictureInPictureAdOptions {
  let options = PictureInPictureAdOptions()
  // Keeps the ad visible beyond the host screen's lifecycle.
  options.presentationScope = .application
  return options
}

Objective-C

- (GADPictureInPictureAdOptions *)createApplicationScopedOptions {
  GADPictureInPictureAdOptions *options =
      [[GADPictureInPictureAdOptions alloc] init];
  // Keeps the ad visible beyond the host screen's lifecycle.
  options.presentationScope = GADPictureInPictureAdPresentationScopeApplication;
  return options;
}

Để biết thêm thông tin, hãy xem bài viết Duy trì khả năng hiển thị của quảng cáo trên nhiều màn hình.

Đặt lệnh gọi lại sự kiện quảng cáo

Để xử lý các sự kiện trong vòng đời của quảng cáo ở chế độ hình trong hình, hãy đặt lệnh gọi lại sự kiện trên quảng cáo trước khi hiển thị. Lệnh gọi lại này báo cáo các sự kiện tiêu chuẩn, chẳng hạn như lượt nhấp và lượt hiển thị. Lệnh gọi lại này cũng báo cáo các sự kiện dành riêng cho chế độ hình trong hình, chẳng hạn như khi quảng cáo hiển thị hoặc bị ẩn:

Swift

func pictureInPictureAdDidShow(_ pictureInPictureAd: PictureInPictureAd) {
  print("Picture-in-Picture ad shown.")
}

func pictureInPictureAdDidHide(_ pictureInPictureAd: PictureInPictureAd) {
  print("Picture-in-Picture ad hidden.")
}

func pictureInPictureAdDidFailToShow(
  _ pictureInPictureAd: PictureInPictureAd, error: Error
) {
  print("Picture-in-Picture ad failed to show: \(error.localizedDescription)")
}

func pictureInPictureAdDidRecordImpression(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad recorded an impression.")
}

func pictureInPictureAdDidRecordClick(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad recorded a click.")
}

func pictureInPictureAdWillPresentScreen(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad will present screen.")
}

func pictureInPictureAdWillDismissScreen(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad will dismiss screen.")
}

func pictureInPictureAdDidDismissScreen(
  _ pictureInPictureAd: PictureInPictureAd
) {
  print("Picture-in-Picture ad dismissed screen.")
}

Objective-C

- (void)pictureInPictureAdDidShow:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad shown.");
}

- (void)pictureInPictureAdDidHide:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad hidden.");
}

- (void)pictureInPictureAdDidFailToShow:(GADPictureInPictureAd *)pictureInPictureAd
                              withError:(NSError *)error {
  NSLog(@"Picture-in-Picture ad failed to show: %@", error);
}

- (void)pictureInPictureAdDidRecordImpression:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad recorded an impression.");
}

- (void)pictureInPictureAdDidRecordClick:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad recorded a click.");
}

- (void)pictureInPictureAdWillPresentScreen:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad will present screen.");
}

- (void)pictureInPictureAdWillDismissScreen:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad will dismiss screen.");
}

- (void)pictureInPictureAdDidDismissScreen:(GADPictureInPictureAd *)pictureInPictureAd {
  NSLog(@"Picture-in-Picture ad dismissed screen.");
}

Ẩn quảng cáo

Để xoá quảng cáo nổi khỏi màn hình, hãy gọi phương thức hide. Phương thức này gọi lệnh gọi lại sự kiện quảng cáo bị ẩn:

Swift

private func hidePictureInPictureAd() {
  // Use the loaded PictureInPictureAd instance.
  guard let pipAd else {
    print("No ad to hide.")
    return
  }
  pipAd.hide()
}

Objective-C

- (void)hidePictureInPictureAd {
  // Use the loaded PictureInPictureAd instance.
  if (!self.pipAd) {
    NSLog(@"No ad to hide.");
    return;
  }
  [self.pipAd hide];
}

Dọn dẹp tài nguyên quảng cáo

Để tránh làm rò rỉ bộ nhớ, hãy thả tham chiếu đến đối tượng quảng cáo khi ứng dụng của bạn hoàn tất việc sử dụng quảng cáo. Ví dụ: khi ứng dụng của bạn không còn hiển thị hoặc tương tác lại với quảng cáo. Đối với quảng cáo có phạm vi là màn hình, hãy thả tham chiếu khi ứng dụng của bạn xoá màn hình lưu trữ khỏi bộ nhớ. Đối với quảng cáo có phạm vi trong ứng dụng, hãy giữ lại giá trị tham chiếu quảng cáo trong khi người dùng di chuyển giữa các màn hình và thả giá trị tham chiếu khi người dùng đóng quảng cáo:

Swift

private func cleanUpPictureInPictureAd() {
  pipAd?.hide()
  pipAd = nil
}

Objective-C

- (void)cleanUpPictureInPictureAd {
  [self.pipAd hide];
  self.pipAd = nil;
}

Đảm bảo quảng cáo hiển thị trên nhiều màn hình

Khi bạn đặt phạm vi trình bày thành ứng dụng, quảng cáo ở chế độ hình trong hình vẫn hiển thị ngay cả khi ứng dụng của bạn xoá màn hình lưu trữ khỏi bộ nhớ. Để tương tác hoặc đóng quảng cáo khi người dùng rời khỏi màn hình lưu trữ, ứng dụng của bạn phải giữ quyền truy cập vào quảng cáo ở chế độ hình trong hình. Bạn nên giữ quảng cáo trong một trình quản lý trạng thái dùng chung hoặc singleton ở cấp ứng dụng thay vì trong biến thực thể của một màn hình.