تضمين إعلانات في وضع "نافذة ضمن النافذة" (إصدار تجريبي)

اختيار النظام الأساسي: Android iOS

نافذة ضمن النافذة

تظهر الإعلانات في وضع "نافذة ضمن النافذة" في نافذة عائمة تبقى في مقدّمة المحتوى على الشاشة، مثل المقالات أو الخلاصات أو طريقة اللعب. يتيح هذا الشكل للمستخدمين التفاعل مع تطبيقك بينما يظل الإعلان مرئيًا. اختَر هذا الشكل لعرض إعلانات لا تستحوذ على الشاشة بأكملها. مزيد من المعلومات حول الإعلانات في وضع "نافذة ضمن النافذة"

يتناول هذا الدليل كيفية طلب إعلانات في وضع "نافذة ضمن النافذة" وعرضها في تطبيقك باستخدام Google Mobile Ads SDK.

قبل البدء

قبل المتابعة، يُرجى اتّباع الخطوات التالية:

تحميل إعلان

لتحميل كائن GADPictureInPictureAd، أنشئ طلب عرض الإعلان واستدعِ الطريقة 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 برقم تعريف وحدتك الإعلانية.

عرض الإعلان

لعرض الإعلان في وضع "نافذة ضمن النافذة" على الشاشة، اضبط خيارات وضع "نافذة ضمن النافذة" واستدعِ الطريقة 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;
}

لمزيد من المعلومات، اطّلِع على مقالة إبقاء الإعلان مرئيًا على جميع الشاشات.

ضبط معاودة الاتصال بحدث الإعلان

للتعامل مع أحداث مراحل نشاط الإعلان في وضع "نافذة ضمن النافذة"، اضبط دالة معاودة الاتصال الخاصة بالحدث على إعلانك قبل عرضه. تعرض دالة معاودة الاتصال هذه الأحداث العادية، مثل النقرات ومرّات الظهور. يُبلغ هذا الإجراء أيضًا عن أحداث خاصة بوضع "نافذة ضمن النافذة"، مثل وقت عرض الإعلان أو إخفائه:

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

إبقاء الإعلان مرئيًا على جميع الشاشات

عند ضبط نطاق العرض التقديمي على التطبيق، يظل الإعلان في وضع "نافذة ضمن النافذة" مرئيًا حتى عندما يزيل تطبيقك شاشة الاستضافة من الذاكرة. للتفاعل مع الإعلان أو إغلاقه عندما ينتقل المستخدم بعيدًا عن الشاشة المضيفة، يجب أن يحتفظ تطبيقك بإمكانية الوصول إلى الإعلان في وضع "نافذة ضمن النافذة". ننصحك بتخزين الإعلان في عنصر أحادي المستوى على مستوى التطبيق أو في أداة إدارة الحالة المشترَكة بدلاً من متغيّر مثيل لشاشة واحدة.