تظهر الإعلانات في وضع "نافذة ضمن النافذة" في نافذة عائمة تبقى في مقدّمة المحتوى على الشاشة، مثل المقالات أو الخلاصات أو طريقة اللعب. يتيح هذا الشكل للمستخدمين التفاعل مع تطبيقك بينما يظل الإعلان مرئيًا. اختَر هذا الشكل لعرض إعلانات لا تشغل الشاشة بأكملها.
يتناول هذا الدليل كيفية طلب وعرض إعلانات "نافذة ضمن النافذة" في تطبيقك باستخدام Google Mobile Ads SDK.
قبل البدء
قبل المتابعة، يُرجى اتّباع الخطوات التالية:
ثبِّت الإصدار Google Mobile Ads SDK
13.10.0أو إصدارًا أحدث.فعِّل الإعلانات الاختبارية واستخدِم رقم تعريف الوحدة الإعلانية الاختبارية التالي:
/21775744923/example/picture-in-picture
تحميل إعلان
لتحميل كائن GADPictureInPictureAd، أنشئ طلب عرض إعلان واستدعِ الطريقة load:
Swift
استبدِل adUnitID برقم تعريف الوحدة الإعلانية.
Objective-C
استبدِل 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; }
إبقاء الإعلان مرئيًا على جميع الشاشات
عند ضبط نطاق العرض التقديمي على التطبيق، يظل الإعلان في وضع "نافذة ضمن النافذة" مرئيًا حتى عندما يزيل تطبيقك شاشة الاستضافة من الذاكرة. للتفاعل مع الإعلان أو إغلاقه عندما ينتقل المستخدم بعيدًا عن الشاشة المضيفة، يجب أن يحتفظ تطبيقك بإمكانية الوصول إلى الإعلان في وضع "نافذة ضمن النافذة". ننصحك بتخزين الإعلان في عنصر أحادي المستوى على مستوى التطبيق أو في أداة إدارة الحالة المشترَكة بدلاً من تخزينه في متغير مثيل لشاشة واحدة.
للاطّلاع على أمثلة حول كيفية إبقاء الإعلان مرئيًا على جميع الشاشات، راجِع التطبيقات النموذجية التالية: