iOS 專用 AdSense 行動應用程式搜尋廣告 (AFSMA) 導入作業

必要條件

本實作指南假設您已熟悉下列項目:

總覽

本文件概述在 iOS 行動應用程式中整合 AdSense 行動應用程式搜尋廣告 (AFSMA) 廣告的流程。AFSMA 廣告有時也稱為動態高度搜尋廣告。若要在 iOS 上請求及顯示 AFSMA 廣告,您必須導入下列項目:

GADSearchBannerView

  • 這個類別繼承自 iOS UIView 類別,並顯示 AFSMA 廣告。GADSearchBannerView 使用 GADDynamicHeightSearchRequest 發出廣告請求,並顯示傳回的廣告。GADSearchBannerView 應新增至應用程式的任何現有檢視畫面;通常這是父項檢視控制器,用於保留新增 GADSearchBannerView 的檢視畫面。適當的委派項目應在 GADSearchBannerView 上設定。
  • GADSearchBannerView 必須使用 initWithAdSize:kGADAdSizeFluid 例項化,才能要求 AFSMA 廣告。使用 initWithAdSize:kGADAdSizeBannerGADSearchBannerView 例項化,會要求舊版 AFSMA 廣告。
  • 這個物件上的 adUnitID 屬性必須設為屬性代碼。

GADDynamicHeightSearchRequest

  • 這個物件會封裝廣告請求參數。這類似於在 AdSense 搜尋廣告電腦版和行動版網站中,在 JavaScript 廣告請求物件 (網頁選項、單元選項) 中設定參數的做法。

(void)adView:(GADBannerView *)bannerView willChangeAdSizeTo:(GADAdSize)size

  • 廣告請求傳回時,系統就會呼叫此回呼。由於傳回的廣告單元可能包含數個包含不同額外資訊的廣告,因此發出廣告請求時,廣告單元的確切大小不明。傳回廣告後,您必須更新橫幅廣告檢視畫面,以符合廣告單元的新大小。用於在父項檢視區塊中調整 GADSearchBannerView 大小的程式碼應在此實作。

導入範例

以下範例說明如何使用 GBannerViewController 建立 GADSearchBannerView 做為 UIScrollView 的子檢視畫面。如要正確要求 AFSMA 廣告,GADSearchBannerView 物件必須使用 initWithAdSize:kGADAdSizeFluid 例項化。

// GBannerViewController.m implementation

@interface GBannerViewController () <GADAdSizeDelegate,
                                     GADBannerViewDelegate>

@property(nonatomic, strong) GADSearchBannerView *searchBannerView;

@property(nonatomic, strong) UIScrollView *scrollView;

@end

@implementation GBannerViewController

- (void)viewDidLoad {
  [super viewDidLoad];

  // Create the scroll view.
  ....
  ....

  // Create the banner.
  self.searchBannerView = [[GADSearchBannerView alloc] initWithAdSize:kGADAdSizeFluid];

  // Replace with your pub ID (e.g. ms-app-pub-9616389000213823).
  self.searchBannerView.adUnitID = @"ms-app-pub-################";

  // Set the initial location and size of the banner. The initial height
  // is set to 0 since we might not get an ad back.
  self.searchBannerView.frame = CGRectMake(0,
                                           0,
                                           CGRectGetWidth(self.view.bounds),
                                           0);
  self.searchBannerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;

  // Set the delegate properties.
  self.searchBannerView.adSizeDelegate = self;
  self.searchBannerView.delegate = self;

  // Add the new search banner into the parent scrollView.
  [self.scrollView addSubview:self.searchBannerView];
  }

在同一個 GBannerViewController 內建立 GADDynamicHeightSearchRequest,指定將在 GADSearchView 中顯示的廣告參數。

// Create a search request and load the banner.
GADDynamicHeightSearchRequest *searchRequest = [[GADDynamicHeightSearchRequest alloc] init];

// Ad request options (set using GADDynamicHeightSearchRequest properties).
searchRequest.query = @"flowers";
searchRequest.numberOfAds = 2;

// Replace with the ID of a style from your custom search styles
[searchRequest setAdvancedOptionValue:@"0000000001"
                               forKey:@"styleId"];

GADDynamicHeightSearchRequest 物件上設定額外屬性,即可使用其他自訂選項

如要提出廣告請求,請使用 GADSearchBannerView 物件的 GADDynamicHeightSearchRequest 物件呼叫 loadRequest

[self.searchBannerView loadRequest:searchRequest];

如要讓父項檢視畫面在廣告傳回後正確容納 GADSearchBannerView,則必須實作以下回呼。

// Callback to update the parent view height.
- (void)adView:(GADBannerView *)bannerView willChangeAdSizeTo:(GADAdSize)size {
  // Update the banner view based on the ad size.
  CGRect newFrame = self.searchBannerView.frame;
  newFrame.size.height = size.size.height;
  self.searchBannerView.frame = newFrame;

  // Perform any additional logic needed due to banner view size change.
  ...
}

進階選項

大多數的廣告請求參數可透過 GADDynamicHeightSearchRequest 物件上的屬性 (上述 searchRequest) 設定。其他參數必須使用與 setAdvancedOptionValue 方法的鍵/值組合進行設定:

// Advanced customization options (set using key-value pair).

// Set a parameter (parameter_name) and its value (parameter_value).
[searchRequest setAdvancedOptionValue:@"parameter_value"
                               forKey:@"parameter_name"];

// Example: Show visible URL below description (domainLinkAboveDescription: false).
[searchRequest setAdvancedOptionValue:@"false"
                               forKey:@"domainLinkAboveDescription"];

查看可用參數的完整清單

調查錯誤

GADBannerViewDelegate 包含回呼,可協助您調查錯誤:

- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error {

  // This callback is triggered when the ad request fails.
  // Add code here to debug the error object to discover causes of failure
  NSLog(@"Ad call failed due to %@", error.userInfo[@"NSUnderlyingError"]);
}

如果廣告請求失敗,您可以使用這個回呼正確處理錯誤,並透過錯誤物件調查錯誤。