기본 요건
이 구현 가이드에서는 사용자가 다음 사항을 잘 알고 있다고 가정합니다.
- iOS 앱 개발
- Google 모바일 광고 SDK를 iOS 프로젝트와 연결 최신 버전의 SDK를 다운로드합니다.
개요
이 문서에서는 iOS 모바일 앱에 AFSMA (모바일 앱용 AFS) 광고를 통합하는 과정을 간략하게 설명합니다. AFSMA 광고는 동적 높이 검색 광고라고도 합니다. iOS에서 AFSMA 광고를 요청하고 렌더링하려면 다음을 구현해야 합니다.
- 이 클래스는 iOS UIView 클래스에서 상속되어 AFSMA 광고를 표시합니다.
GADSearchBannerView
는GADDynamicHeightSearchRequest
가 포함된 광고를 요청하고 반환된 광고를 렌더링합니다.GADSearchBannerView
는 앱의 기존 뷰에 추가되어야 합니다. 일반적으로GADSearchBannerView
가 추가되는 뷰를 보유하는 상위 뷰 컨트롤러입니다. 적절한 위임을GADSearchBannerView
에 설정해야 합니다. - AFSMA 광고를 요청하려면
GADSearchBannerView
를initWithAdSize:kGADAdSizeFluid
로 인스턴스화해야 합니다.initWithAdSize:kGADAdSizeBanner
로GADSearchBannerView
를 인스턴스화하면 기존 AFSMA 광고가 요청됩니다. - 이 객체의
adUnitID
속성은 속성 코드로 설정해야 합니다.
- 이 객체는 광고 요청 매개변수를 캡슐화합니다. 이는 AFS 데스크톱 및 모바일 웹의 자바스크립트 광고 요청 객체 (페이지 옵션, 단위 옵션)에서 매개변수를 설정하는 것과 유사합니다.
(void)adView:(GADBannerView *)bannerView willChangeAdSizeTo:(GADAdSize)size
- 이 콜백은 광고 요청이 반환되면 호출됩니다. 반환된 광고 단위에는 광고 확장이 서로 다른 여러 광고가 포함될 수 있으므로 광고 요청 시 광고 단위의 정확한 크기를 알 수 없습니다. 광고가 반환되면
새로운 광고 단위 크기를 수용할 수 있도록
배너 보기를 업데이트해야 합니다. 상위 뷰에서
GADSearchBannerView
의 크기를 조절하는 코드를 여기에 구현해야 합니다.
구현 예시
아래 예는 GBannerViewController
를 사용하여 GADSearchBannerView
를 UIScrollView
의 하위 뷰로 만드는 방법을 보여줍니다. 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
메서드와 함께 키-값 쌍을 사용하여 설정해야 합니다.
// 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"]);
}
광고 요청이 실패하면 이 콜백을 사용하여 오류를 적절히 처리하고 오류 객체를 통해 오류를 조사할 수 있습니다.