הטמעה של מודעות מסוג 'תמונה בתוך תמונה' (בטא)

בחירת פלטפורמה: Android iOS

תמונה בתוך תמונה

מודעות מסוג "תמונה בתוך תמונה" (PiP) מוצגות בחלון צף שמופיע מעל התוכן במסך, כמו כתבות, פידים או משחקים. הפורמט הזה מאפשר למשתמשים לבצע אינטראקציה עם האפליקציה בזמן שהמודעה נשארת גלויה. כדי להציג מודעות שלא תופסות את כל המסך, בוחרים בפורמט הזה. מידע נוסף על מודעות בתמונה בתוך תמונה

במדריך הזה מוסבר איך לשלוח בקשה להצגת מודעות במצב 'תמונה בתוך תמונה' באפליקציה באמצעות Google Mobile Ads SDK.

לפני שמתחילים

לפני שממשיכים, צריך:

טעינת מודעה

כדי לטעון אובייקט GADPictureInPictureAd, יוצרים בקשה להצגת מודעה ומבצעים קריאה ל-method‏ 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)")
  }
}

מחליפים את adUnitID במזהה יחידת המודעות.

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

מחליפים את kAdUnitID במזהה יחידת המודעות.

הצגת המודעה

כדי להציג את המודעה במצב תמונה בתוך תמונה על המסך, צריך להגדיר את האפשרויות של התמונה בתוך תמונה ולהפעיל את ה-method‏ show. בדוגמה הבאה מוגדר מיקום ברירת המחדל של המודעה והיקף ההצגה למסך:

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

הגדרת המיקום

כברירת מחדל, כשמודעה מוצגת בפעם הראשונה ב-Google Mobile Ads SDK, היא מופיעה בתצוגת תמונה בתוך תמונה בפינה השמאלית התחתונה של המסך, או במיקום האחרון הידוע אם היא הוצגה בעבר. כדי להתאים אישית את המיקום שבו המודעה מופיעה, מגדירים את המיקום באפשרויות של תמונה בתוך תמונה. בדוגמה הבאה מוגדר המיקום בחלק העליון של התוכן בפינה הימנית העליונה של המסך:

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

כאן אפשר לראות את כל המשרות הפנויות: GADPictureInPictureAdPosition.

הגדרת היקף ההצגה

כברירת מחדל, Google Mobile Ads SDK קושר מודעה במצב תמונה בתוך תמונה למסך המארח הנוכחי. Google Mobile Ads SDK סוגר את המודעה כשהיררכיית התצוגות של המסך המארח כבר לא נמצאת בזיכרון. כדי שהמודעה תמשיך להיות גלויה אחרי שהמסך המארח יוסר מהזיכרון, צריך להגדיר את היקף ההצגה לאפליקציה:

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

מידע נוסף מופיע במאמר הצגת המודעה בכל המסכים.

הגדרת קריאה חוזרת (callback) לאירוע מודעה

כדי לטפל באירועים במחזור החיים של מודעות בתצוגת תמונה בתוך תמונה, צריך להגדיר את הקריאה החוזרת (callback) של האירוע במודעה לפני שמציגים אותה. פונקציית הקריאה החוזרת הזו מדווחת על אירועים רגילים, כמו קליקים וחשיפות. הקריאה החוזרת הזו מדווחת גם על אירועים ספציפיים למצב 'תמונה בתוך תמונה', כמו מתי המודעה מוצגת או מוסתרת:

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

הסתרת המודעה

כדי להסיר את המודעה הצפה מהמסך, מפעילים את השיטה hide. השיטה הזו מפעילה את הקריאה החוזרת של אירוע הסתרת המודעה:

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

מחיקת משאבי המודעות

כדי למנוע דליפות זיכרון, צריך להסיר את ההפניה לאובייקט המודעה כשהאפליקציה מסיימת להשתמש במודעה. לדוגמה, כשהאפליקציה לא מציגה יותר את המודעה או לא מקיימת איתה אינטראקציה. לגבי מודעות בהיקף מסך, צריך להסיר את ההפניה כשהאפליקציה מסירה את מסך המארח מהזיכרון. לגבי מודעות בהיקף האפליקציה, צריך לשמור את ההפניה למודעה בזמן שהמשתמש עובר בין המסכים, ולהסיר את ההפניה כשהמשתמש סוגר את המודעה:

Swift

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

Objective-C

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

המודעה תישאר גלויה בכל המסכים

כשמגדירים את היקף ההצגה לאפליקציה, המודעה בתמונה בתוך תמונה נשארת גלויה גם כשהאפליקציה מסירה את מסך האירוח מהזיכרון. כדי שהמשתמש יוכל לקיים אינטראקציה עם המודעה או לסגור אותה כשהוא יוצא ממסך האפליקציה המארחת, האפליקציה צריכה לשמור על הגישה למודעה במצב 'תמונה בתוך תמונה'. מומלץ להחזיק את המודעה בסינגלטון ברמת האפליקציה או במנהל מצב משותף, ולא במשתנה מופע של מסך יחיד.