原生廣告選項

原生廣告提供許多進階功能 量身打造最適合的廣告體驗本指南說明 如何使用原生廣告的進階功能

必要條件

資產控制選項

偏好的媒體顯示比例控制項

媒體顯示比例控制項可讓您指定偏好設定 廣告素材。

設定「GADNativeAdMediaAdLoaderOptions mediaAspectRatio」 使用 GADMediaAspectRatio

  • 如未設定,傳回的廣告可以採用任何媒體顯示比例。

  • 設定後,只要指定 偏好的顯示比例類型

以下範例會指示 SDK 優先採用含有 挑選出適當的顯示比例

GADNativeAdMediaAdLoaderOptions *nativeOption = [[GADNativeAdMediaAdLoaderOptions alloc] init];
nativeOption.mediaAspectRatio = GADMediaAspectRatioAny;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

圖片下載控制項

透過圖片下載控制項,您可以決定圖片素材資源是還是只有 URI SDK 傳回的 IP 位址

設定「GADNativeAdImageAdLoaderOptions disableImageLoading」 並指定 BOOL 值。
  • 圖片下載控制項預設為停用。

  • 停用後,Google Mobile Ads SDK 會為您填入圖片和 URI。

  • 啟用後,SDK 只會填入 URI,方便您下載 自訂圖片。

下列範例會指示 SDK 只傳回 URI。

GADNativeAdImageAdLoaderOptions *nativeOption = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOption.disableImageLoading = YES;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

映像檔酬載控制項

某些廣告內含一系列的圖片,而非單一廣告。這項功能可用來 指出應用程式是否已準備好顯示所有圖片或只顯示一張。

設定「GADNativeAdImageAdLoaderOptions shouldRequestMultipleImages」 並指定 BOOL 值。
  • 映像檔酬載控制項預設為停用。

  • 停用後,應用程式會指示 SDK 只提供 第一張圖片。

  • 啟用後,應用程式表示已準備好顯示所有圖片 屬性含有多個素材資源

以下範例會指示 SDK 傳回多個圖片素材資源。

GADNativeAdImageAdLoaderOptions *nativeOption = [[GADNativeAdImageAdLoaderOptions alloc] init];
nativeOption.shouldRequestMultipleImages = YES;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOption ]];

AdChoices 刊登位置

AdChoices 位置控制項

AdChoices 位置控制項可讓你選擇要在哪個角落 AdChoices 圖示。

設定「GADNativeAdViewAdOptions preferredAdChoicesPosition」 使用 GADAdChoicesPosition 值。

  • 如未設定,AdChoices 圖示位置會設為右上方。

  • 如果已設定,AdChoices 會顯示在自訂位置,

下例說明如何設定自訂 AdChoices 圖片位置。

GADNativeAdViewAdOptions *nativeOptions = [[GADNativeAdViewAdOptions alloc] init];
nativeOptions.preferredAdChoicesPosition = GADAdChoicesPositionTopLeftCorner;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

AdChoices 自訂檢視畫面

AdChoices 自訂檢視功能可讓您將 AdChoices 圖示放置在 自訂位置。這與只有 AdChoices 位置控制項不同 允許指定四個角的其中一個

設定 GADNativeAd.adChoicesView 含有 GADAdChoicesView 的資源 這樣就會觸發 AdChoices 內容, GADAdChoicesView

下例說明如何設定自訂 AdChoices 檢視畫面。 AdChoices 圖示會顯示在 GADAdChoicesView 中。

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader didReceiveNativeAd:(GADNativeAd *)nativeAd {

    ...

    
    GADAdChoicesView *customAdChoicesView =
        [[GADAdChoicesView alloc] initWithFrame: CGRectMake(..., ..., ..., ...)];

    [nativeAdView addSubview:customAdChoicesView];
    nativeAdView.adChoicesView = customAdChoicesView;

    // Associate the native ad view with the native ad object. This is
    // required to make the ad clickable.
    // Note: this should always be done after populating the ad views.
    nativeAdView.nativeAd = nativeAd;
}

Swift

func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
    refreshAdButton.isEnabled = true

    ...
    // Define a custom position for the AdChoices icon.
    let customRect = CGRect(x: 100, y: 100, width: 15, height: 15)
    let customAdChoicesView = GADAdChoicesView(frame: customRect)
    nativeAdView.addSubview(customAdChoicesView)
    nativeAdView.adChoicesView = customAdChoicesView

    // Associate the native ad view with the native ad object. This is
    // required to make the ad clickable.
    // Note: this should always be done after populating the ad views.
    nativeAdView.nativeAd = nativeAd;
}

影片控制選項

啟動靜音行為

啟動靜音行為後,即可停用或啟用影片的起始音訊。

設定「GADVideoOptions startMuted」 並指定 BOOL 值。
  • 啟動靜音行為預設為啟用。

  • 如果停用這項設定,應用程式會要求播放影片 音訊。

  • 啟用後,您的應用程式會要求將影片開頭設為靜音。

以下範例說明如何以取消靜音的音效開始播放影片。

GADVideoOptions *nativeOptions = [[GADVideoOptions alloc] init];
nativeOptions.startMuted = NO;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

自訂播放控制項

這樣您就能要求自訂影片輸入控制項來播放、暫停或靜音 影片。

設定「GADVideoOptions customControlsRequested」 並指定 BOOL 值。
  • 自訂播放控制項預設為停用,

  • 停用後,影片就會顯示 SDK 轉譯的輸入控制項。

  • 如果廣告已啟用影片內容和自訂控制項,您應該 然後同時顯示自訂控制項與廣告,因為廣告不會實際放送 控制項本身接著,控制項可以呼叫 GADVideoController

以下範例說明如何透過自訂播放控制項要求影片。

GADVideoOptions *nativeOptions = [[GADVideoOptions alloc] init];
nativeOptions.customControlsRequested = YES;

GADAdLoader* adLoader = [[GADAdLoader alloc] initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
                                           rootViewController:self
                                                      adTypes:@[ GADAdLoaderAdTypeNative ]
                                                      options:@[ nativeOptions ]];

檢查是否已啟用自訂控制項

因為在請求時無法得知,傳回的廣告是否將 自訂影片控制項,您必須確認是否已啟用自訂控制項。

Objective-C

- (void)adLoader:(GADAdLoader *)adLoader
    didReceiveNativeAd:(GADNativeAd*)nativeAd {

  GADVideoController *videoController = nativeAd.mediaContent.videoController;
  BOOL canShowCustomControls = videoController.customControlsEnabled;

}

Swift

func adLoader(_ adLoader: GADAdLoader, didReceive nativeAd: GADNativeAd) {
        let videoController = nativeAd.mediaContent.videoController
        let canShowCustomControls = videoController?.customControlsEnabled() == true
}

自訂點擊手勢

自訂點擊手勢是原生廣告功能,使用者只要滑動廣告檢視畫面, 。可與採用滑動手勢的應用程式搭配使用 用來進行內容導覽的手勢本指南說明如何啟用自訂點擊 對原生廣告套用手勢

初始化 GADNativeAdCustomClickGestureOptions 例項。您也必須指出 必須設為點擊

  • 自訂點擊手勢預設為停用。

  • 如果停用,系統只會將輕觸動作計為點擊次數。

  • 啟用後,滑動手勢就會計為點擊。 是否會同時將輕觸動作計為點擊次數。

以下範例說明如何將自訂滑動手勢實作 並保留一般的輕觸行為

GADNativeAdCustomClickGestureOptions *swipeGestureOptions = [[GADNativeAdCustomClickGestureOptions alloc]
        initWithSwipeGestureDirection:UISwipeGestureRecognizerDirectionRight
                          tapsAllowed:YES];

// The following sample ad unit ID has been enabled for custom click gestures
// and can be used for testing.
self.adLoader = [[GADAdLoader alloc]
        initWithAdUnitID:@"ca-app-pub-3940256099942544/3986624511"
      rootViewController:self
                 adTypes:@[ GADAdLoaderAdTypeNative ]
                 options:@[ swipeGestureOptions ]];

監聽滑動手勢事件

記錄滑動手勢點擊時,Google Mobile Ads SDK 會叫用 nativeAdDidRecordSwipeGestureClick:敬上 GADNativeAdDelegate 的委派方法 ,除了現有的 nativeAdDidRecordClick: 以外, 委派方法。

#pragma mark - GADNativeAdDelegate implementation

// Called when a swipe gesture click is recorded.
- (void)nativeAdDidRecordSwipeGestureClick:(GADNativeAd *)nativeAd {
  NSLog(@"A swipe gesture click has occurred.");
}

// Called when a swipe gesture click or a tap click is recorded, as configured in
// GADNativeAdCustomClickGestureOptions.
- (void)nativeAdDidRecordClick:(GADNativeAd *)nativeAd {
  NSLog(@"A swipe gesture click or tap click has occurred.");
}

中介服務

自訂點擊手勢僅適用於 Google 行動服務的原生廣告 廣告 SDK 呈現。具有 需要使用第三方 SDK 請不要回應自訂點擊路線設定。