子母畫面 (PiP) 廣告會顯示在浮動視窗中,並置於螢幕內容 (例如文章、動態消息或遊戲畫面) 的頂端。使用者可與應用程式互動,同時看到廣告。如要顯示不會佔用整個畫面的廣告,請選擇這個格式。進一步瞭解子母畫面廣告。
本指南說明如何使用 Google Mobile Ads SDK,在應用程式中要求及顯示子母畫面廣告。
事前準備
繼續操作前,請先執行下列工作:
安裝 Google Mobile Ads SDK
13.10.0以上版本。啟用測試廣告,並使用下列測試廣告單元 ID:
ca-app-pub-3940256099942544/8810945611
載入廣告
如要載入 GADPictureInPictureAd 物件,請建立廣告請求並呼叫 load 方法:
Swift
將 adUnitID 替換為廣告單元 ID。
Objective-C
將 kAdUnitID 替換為廣告單元 ID。
顯示廣告
如要在畫面上顯示子母畫面廣告,請設定子母畫面選項並呼叫 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; }
確保廣告在各種螢幕上都能顯示
如果將顯示範圍設為應用程式,即使應用程式從記憶體中移除主機畫面,子母畫面廣告仍會顯示。使用者離開主機畫面時,如要與廣告互動或關閉廣告,應用程式必須保留子母畫面廣告的存取權。建議您將廣告保留在應用程式層級的單例模式或共用狀態管理工具中,而不是單一畫面的執行個體變數中。