從 iOS 13 開始,應用程式可以支援 iPad 上的多個視窗,也就是說,使用者可以同時與多個應用程式 UI 副本互動。每個視窗的大小都不同,且隨時可以調整大小,這會影響廣告的載入和顯示方式。
本指南旨在說明在 iPad 多視窗情境中,正確呈現廣告的最佳做法。
必要條件
- Google Mobile Ads SDK 7.53.0 以上版本
- 在專案中啟用場景支援功能
- 導入至少一種廣告格式
在廣告請求中設定場景
如要接收符合特定視窗的廣告,請將檢視區塊的 windowScene
傳遞至廣告請求。Google Mobile Ads SDK 會傳回適合該場景大小的廣告。
Swift
func loadInterstitial() { let request = Request() request.scene = view.window?.windowScene InterstitialAd.load(with: "[AD_UNIT_ID]", request: request) { ad, error in } }
Objective-C
- (void)loadInterstitial { GADRequest *request = [GADRequest request]; request.scene = self.view.window.windowScene; [GADInterstitialAd loadWithAdUnitID:@"[AD_UNIT_ID]" request:request completionHandler:^(GADInterstitialAd *ad, NSError *error) {}]; }
在測試模式中,如果多場景應用程式在未傳遞場景的情況下要求廣告,廣告請求就會失敗,並顯示下列錯誤:
<Google> Invalid Request. The GADRequest scene property should be set for
applications that support multi-scene. Treating the unset property as an error
while in test mode.
在正式版模式中,廣告請求會填滿,但如果廣告要顯示在非全螢幕視窗中,就會無法顯示。本例中的錯誤訊息如下:
<Google> Ad cannot be presented. The full screen ad content size exceeds the current window size.
在 viewDidAppear 中建立廣告請求:
多視窗案例會要求您必須有視窗場景,才能傳送廣告請求。由於檢視區塊尚未新增至 viewDidLoad:
中的視窗,您應在 viewDidAppear:
中建構廣告請求,此時視窗場景已設定完畢。
請注意,在應用程式生命週期中,viewDidAppear:
可能會被呼叫多次。建議您將廣告請求初始化程式碼包裝在旗標中,指出是否已完成初始化。
Swift
override func viewDidAppear(_ animated: Bool) { super.viewDidAppear(animated) if !requestInitialized { loadInterstitial() requestInitialized = true } }
Objective-C
- (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; if (!_requestInitialized) { [self loadInterstitial]; _requestInitialized = YES; } }
調整控點大小
使用者隨時可以拖曳場景,並在提出廣告請求後變更視窗大小。您可自行決定在發生大小調整時是否要求新廣告。
下列範例程式碼使用 viewWillTransitionToSize:withTransitionCoordinator:
,在根檢視區塊控制器的視窗旋轉或調整大小時收到通知,但您也可以監聽 windowScene:didUpdateCoordinateSpace:interfaceOrientation:traitCollection:
,瞭解視窗場景的特定變更。
插頁式廣告和獎勵廣告
Google Mobile Ads SDK 提供 canPresentFromViewController:error:
方法,可判斷插頁式廣告或獎勵廣告是否有效,讓您在視窗大小變更時,檢查是否需要重新整理任何全螢幕廣告。
Swift
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: coordinator) coordinator.animate(alongsideTransition: nil) { [self] context in do { try interstitial?.canPresent(from: self) } catch { loadInterstitial() } } }
Objective-C
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:nil completion:^(id _Nonnull context) { if (![self.interstitial canPresentFromRootViewController:self error:nil]) { [self loadInterstitial]; } }]; }
橫幅廣告
處理視窗大小調整的方式與視窗旋轉相同。應用程式有責任確保橫幅廣告符合新視窗大小。
以下範例會使用新的視窗寬度建立自動調整式橫幅廣告:
Swift
override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) { super.viewWillTransition(to: size, with: coordinator) coordinator.animate(alongsideTransition: nil) { [self] context in loadBanner() } } func loadBanner() { let bannerWidth = view.frame.size.width bannerView.adSize = currentOrientationAnchoredAdaptiveBanner(width: bannerWidth) let request = Request() request.scene = view.window?.windowScene bannerView.load(request) }
Objective-C
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id)coordinator { [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator]; [coordinator animateAlongsideTransition:nil completion:^(id _Nonnull context) { [self loadBannerAd]; }]; } - (void)loadBannerAd { CGFloat bannerWidth = self.view.frame.size.width; self.bannerView.adSize = GADCurrentOrientationAnchoredAdaptiveBannerAdSizeWithWidth(bannerWidth); GADRequest *request = [GADRequest request]; request.scene = self.view.window.windowScene; [self.bannerView loadRequest:request]; }
原生廣告
您可以控制原生廣告的算繪作業,並負責確保原生廣告是在調整大小後的檢視區塊中算繪,與應用程式的其餘內容類似。
已知問題
目前多視窗和分割畫面廣告僅支援直向模式。在橫向模式下請求廣告時,您會收到下列記錄訊息。
<Google> Ad cannot be presented. The full screen ad content size exceeds the
current window size.