iOS 用の AFS(モバイルアプリ)の実装

Prerequisites

この実装ガイドは、次に精通していることを前提としています。

概要

このドキュメントでは、iOS モバイルアプリに AFS(モバイルアプリ)広告を統合するプロセスの概要を説明します。AFSMA 広告は、動的高さの検索広告と呼ばれることもあります。iOS で AFSMA 広告をリクエストしてレンダリングするには、以下を実装する必要があります。

GADSearchBannerView

  • このクラスは iOS UIView クラスを継承し、AFSMA 広告を表示しています。GADSearchBannerViewGADDynamicHeightSearchRequest を使って広告のリクエストを行い、返された広告をレンダリングします。GADSearchBannerView は、アプリの既存のビューのいずれかに追加する必要があります。通常は、GADSearchBannerView が追加されたビューを保持する親ビュー コントローラです。適切なデリゲートは GADSearchBannerView に設定する必要があります。
  • AFSMA 広告をリクエストするには、GADSearchBannerViewinitWithAdSize:kGADAdSizeFluid でインスタンス化する必要があります。initWithAdSize:kGADAdSizeBannerGADSearchBannerView をインスタンス化すると、従来の AFSMA 広告をリクエストできます。
  • このオブジェクトの adUnitID プロパティをプロパティ コードに設定する必要があります。

GADDynamicHeightSearchRequest

  • このオブジェクトは、広告リクエスト パラメータをカプセル化します。これは、AFS のデスクトップおよびモバイルウェブ向けの JavaScript 広告リクエスト オブジェクト(ページ オプション、ユニット オプション)でのパラメータの設定に似ています。

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

  • このコールバックは、広告リクエストが返された時点で呼び出されます。返される広告ユニットには、異なる広告表示オプションを含む複数の広告が含まれることがあるため、広告リクエストが行われたときの広告ユニットの正確なサイズは不明です。広告が返されたら、広告ユニットの新しいサイズに合わせてバナービューを更新する必要があります。親ビューの GADSearchBannerView のサイズを変更するコードをここに実装する必要があります。

実装例

以下の例は、GBannerViewController を使用して UIScrollView のサブビューとして GADSearchBannerView を作成する方法を示しています。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 内で、GADSearchView でレンダリングされる広告のパラメータを指定する GADDynamicHeightSearchRequest を作成します。

// 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 メソッドで Key-Value ペアを使用して設定する必要があります。

// 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"]);
}

広告リクエストが失敗した場合は、このコールバックを使用してエラーを適切に処理し、エラー オブジェクトでエラーを調査できます。