iOS 向けの AFS(モバイルアプリ)の導入

前提条件

この実装ガイドは、読者が次の内容を理解していることを前提としています。

概要

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

GADSearchBannerView

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

GADDynamicHeightSearchRequest

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

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

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

実装例

以下の例では、GBannerViewController を使用して、GADSearchBannerViewUIScrollView のサブビューとして作成しています。AFSMA 広告を適切にリクエストするには、initWithAdSize:kGADAdSizeFluid を使用して GADSearchBannerView オブジェクトをインスタンス化する必要があります。

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

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