Google 移动广告 SDK 还支持自定义搜索广告样式。如果您的应用已在使用 Google 移动广告 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,请参阅其官方文档,了解如何创建和使用 Podfile。
概览
如果您要从 2.0.8 或更低版本升级到 4.0 或更高版本,请参阅我们的迁移指南。
本文档简要介绍了在 iOS 移动应用中集成 AFS 原生广告的流程。
- 您需要向
GANSearchAdController
构造函数提供发布商的网络媒体资源代码、所需的设置 ID 以及关联的GANSearchAdControllerOptions
对象。 - 每次调用
loadAds()
都表示一次新的搜索,这会导致当前的一组广告被舍弃并失效。 - 广告素材存储在
GANAdView
中。 - 您可以通过
populateAdView
方法将广告插入广告GANAdView
。除了要填充的GANAdView
之外,调用方还会提供一个adIdentifier
,该字符串是应唯一标识广告的任意字符串。在 API 中,系统会为传入的每个 adIdentifier 分配一个特定广告。之后,每当再次传递该 adKey 时,系统就会返回相同的广告。例如,如果首次使用 adIdentifier“keyA”调用populateAdView
,则后续每次调用populateAdView
并同时将“keyA”作为adIdentifier
传递时,都会导致同一个广告得到展示。
- 这是包含该广告素材的 UIView。
- 对
GANSearchAdController
使用populateAdView
方法,用广告填充此视图。
- 将此对象传递给
GANSearchAdController
构造函数,可指定如何请求和展示广告的行为。
- 使用此对象在
GANSearchAdController
实例上调用loadAds
方法可发出广告请求。
- 实现此接口并将其提供给
GANSearchAdController
,以便为多种状态注册回调。
实现示例
下面的示例演示了如何在示例 ViewController
中创建 GANSearchAdController
和 GANView
以展示广告。
// 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"];
}
}
...