Google Mobile Ads SDK 也支援自訂搜尋樣式。如果您的應用程式已使用 Google Mobile Ads SDK,建議您改用 AFSMA SDK 版本。
必要條件
- 使用 Xcode 8.0 以上版本
- 指定 iOS 8.0 以上版本
- 有效的發布商網站資源代碼 (例如 vert-ppa-test1-srp)
- CocoaPods
匯入 AFS 原生 SDK
CocoaPods
使用 CocoaPods 將 Google-AFSNative SDK 匯入 iOS 專案。開啟專案的 Podfile,然後將下面這一行新增至應用程式的目標:
pod 'Google-AFSNative'
然後,透過指令列執行下列指令:
pod install --repo-update
如果您是第一次使用 CocoaPods,請參閱其官方說明文件,瞭解如何建立和使用 Podfiles。
總覽
如要從 2.0.8 以下版本升級至 4.0 以上版本,請參閱遷移指南。
本文件概述在 iOS 行動應用程式中整合 AdSense 搜尋廣告原生廣告的程序。
GANSearchAdController
建構函式必須提供發布商的網站資源代碼、所需設定 ID 和相關聯的GANSearchAdControllerOptions
物件。- 每次呼叫
loadAds()
都代表新的搜尋,而且會捨棄目前這組廣告並失效。 - 廣告素材儲存在「
GANAdView
」中。 - 系統會使用
populateAdView
方法在廣告GANAdView
中插入廣告。除了要填入的GANAdView
外,呼叫端也會提供adIdentifier
,這是一個可明確識別廣告的任意字串。在 API 中,系統會將特定廣告指派給傳入的每個 adIdentifier。接著,只要日後再次傳遞該 adKey,系統就會傳回相同的廣告。舉例來說,如果系統第一次使用 adIdentifier「keyA」呼叫populateAdView
,那麼每次傳遞「keyA」做為adIdentifier
時呼叫populateAdView
,都會導致相同的廣告。
- 這是包含該廣告素材的 UIView。
- 請在
GANSearchAdController
上使用populateAdView
方法,以廣告填入這個檢視畫面。
- 將這個物件傳遞至
GANSearchAdController
建構函式,以指定廣告要求及顯示方式的行為。
- 使用這個物件的
GANSearchAdController
例項呼叫loadAds
方法,發出廣告請求。
- 實作此介面並提供給
GANSearchAdController
,以註冊多種狀態的回呼。
導入範例
下例說明如何建立 GANSearchAdController
和 GANView
,以便在範例 ViewController
顯示廣告。
// SampleAppViewController.m implementation
#import <AFSNative/AFSNative.h>
@interface GBannerViewController () {
// The Ad Controller used by the sample application.
GANSearchAdController *_adController;
// The Ad View to display the loaded ad.
GANAdView *_adView;
}
// scrollView will be where we place our ads in this example.
@property(nonatomic, strong) UIScrollView *scrollView;
@end
...
- (void)viewDidLoad {
[super viewDidLoad];
// Create the scroll view.
...
[self.view addSubview:scrollView];
// Create a test button and link the ad request to its action.
UIButton *loadBannerButton = [UIButton buttonWithType:UIButtonTypeCustom];
...
[loadBannerButton addTarget:self
action:@selector(loadAd:)
forControlEvents:UIControlEventTouchUpInside];
[self.scrollView addSubview:loadBannerButton];
// Construct the Ad Controller.
GANSearchAdControllerOptions *options = [[GANSearchAdControllerOptions alloc] init];
options.prefetchEnabled = YES;
options.adType = GANSearchAdTypeSPA;
options.adFetchCount = 3;
_adController = [[GANSearchAdController alloc]
initWithPublisherID: @"your-client-id"
styleID: @"your-settings-id"
options: options
delegate: self];
_adView = [_adController adView];
[self.scrollView addSubview:_adView];
}
// Request ads when the test button is pressed.
- (void)loadAd:(id)sender {
// Construct the Ad Request.
GANSearchAdRequest *adRequest = [[GANSearchAdRequest alloc] init];
adRequest.query = @"some-query";
// Start loading ads. Note that the loading is asynchronous.
[_adController loadAds: adRequest];
}
// Insert ads into GANAdView if the request returns successfully.
- (void)searchAdController:(GANSearchAdController *)adController
didLoadAds:(NSInteger)numberOfAds {
if (numberOfAds <= 0) {
NSLog(@"No ads found on the server");
} else {
[_adController populateAdView:_adView identifier:@"demoAd"];
}
}
...