iOS용 AFSMA (모바일 앱용 AFS) 구현

기본 요건

이 구현 가이드에서는 사용자가 다음 사항을 잘 알고 있다고 가정합니다.

개요

이 문서에서는 iOS 모바일 앱에 AFSMA (모바일 앱용 AFS) 광고를 통합하는 과정을 간략하게 설명합니다. AFSMA 광고는 동적 높이 검색 광고라고도 합니다. iOS에서 AFSMA 광고를 요청하고 렌더링하려면 다음을 구현해야 합니다.

GADSearchBannerView

  • 이 클래스는 iOS UIView 클래스에서 상속되어 AFSMA 광고를 표시합니다. GADSearchBannerViewGADDynamicHeightSearchRequest가 포함된 광고를 요청하고 반환된 광고를 렌더링합니다. GADSearchBannerView는 앱의 기존 뷰에 추가되어야 합니다. 일반적으로 GADSearchBannerView가 추가되는 뷰를 보유하는 상위 뷰 컨트롤러입니다. 적절한 위임을 GADSearchBannerView에 설정해야 합니다.
  • AFSMA 광고를 요청하려면 GADSearchBannerViewinitWithAdSize:kGADAdSizeFluid로 인스턴스화해야 합니다. initWithAdSize:kGADAdSizeBannerGADSearchBannerView를 인스턴스화하면 기존 AFSMA 광고가 요청됩니다.
  • 이 객체의 adUnitID 속성은 속성 코드로 설정해야 합니다.

GADDynamicHeightSearchRequest

  • 이 객체는 광고 요청 매개변수를 캡슐화합니다. 이는 AFS 데스크톱 및 모바일 웹의 자바스크립트 광고 요청 객체 (페이지 옵션, 단위 옵션)에서 매개변수를 설정하는 것과 유사합니다.

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

  • 이 콜백은 광고 요청이 반환되면 호출됩니다. 반환된 광고 단위에는 광고 확장이 서로 다른 여러 광고가 포함될 수 있으므로 광고 요청 시 광고 단위의 정확한 크기를 알 수 없습니다. 광고가 반환되면 새로운 광고 단위 크기를 수용할 수 있도록 배너 보기를 업데이트해야 합니다. 상위 뷰에서 GADSearchBannerView의 크기를 조절하는 코드를 여기에 구현해야 합니다.

구현 예시

아래 예는 GBannerViewController를 사용하여 GADSearchBannerViewUIScrollView의 하위 뷰로 만드는 방법을 보여줍니다. AFSMA 광고를 올바르게 요청하려면 initWithAdSize:kGADAdSizeFluidGADSearchBannerView 객체를 인스턴스화해야 합니다.

// 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 메서드와 함께 키-값 쌍을 사용하여 설정해야 합니다.

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

광고 요청이 실패하면 이 콜백을 사용하여 오류를 적절히 처리하고 오류 객체를 통해 오류를 조사할 수 있습니다.