广告信息流
广告信息流是由一系列广告组成的单元。 信息流可以作为应用的主要内容添加,或者添加到现有内容之后。 信息流广告可能包含数十个广告,这些广告分批进行加载,一次加载多个。
本指南介绍了将广告信息流集成到 iOS 应用的过程。 除了代码示例和说明之外,它还包含特定格式的建议以及指向其他资源的链接。
外观
前提条件
- 按照快速入门 中描述的 SDK 集成步骤进行操作。
- 首先,您需要初始化 广告 SDK。
- 确保使用最新版 Yandex Mobile Ads SDK,如果使用的是聚合,则使用最新版统一构建。
实施
集成广告信息流的关键步骤:
- 使用
FeedAdAppearance对象配置广告信息流的外观。 - 创建
FeedAd对象,设置其委托,并实施所需的FeedAdDelegate方法。 - 加载广告。
- 创建
FeedAdCollectionViewAdapter,并将其附加到UICollectionView。
广告信息流集成的具体细节
- 所有 Yandex Mobile Ads SDK 方法调用都必须在主线程中进行。
- 我们建议您在发生广告交互的屏幕的整个生命周期内保持对
FeedAd和FeedAdCollectionViewAdapter对象的强引用。
外观定制
创建名为 FeedAdAppearance 的广告信息流的外观配置对象。
可用的参数如下:
cardWidth(必需):以点为单位设置所投放广告的宽度。 根据屏幕宽度减去所需的侧边距来计算参数值。cardCornerRadius:以点为单位设置所投放广告的圆角半径。
let feedMargin: CGFloat = 24
let cardWidth = view.bounds.width - 2 * feedMargin
let appearance = FeedAdAppearance(cardWidth: cardWidth, cardCornerRadius: 16)
加载广告
创建名为 FeedAd 的广告信息流对象。 为此,请使用从 Yandex Advertising Network 界面获取的广告版位 ID (adUnitId)。
您可以使用 AdRequest 扩展广告请求参数。 为此,请在请求中传递有关用户兴趣、页面上下文、位置以及其他附加数据的信息。 在广告请求中添加额外的上下文信息可以极大地提高广告质量。 如需详细了解,请参阅广告定位。
设置委托并实施 FeedAdDelegate 方法,以接收有关广告加载结果和广告事件的通知:
final class FeedAdViewController: UIViewController {
private var feedAd: FeedAd?
func loadAd() {
let request = AdRequest(adUnitID: "R-M-XXXXXX-Y")
let appearance = FeedAdAppearance(cardWidth: view.bounds.width)
let feedAd = FeedAd(requestConfiguration: request, appearance: appearance)
feedAd.delegate = self
self.feedAd = feedAd
feedAd.loadAd()
}
}
extension FeedAdViewController: FeedAdDelegate {
func feedAdDidLoad(_ feedAd: FeedAd) {
// Called when an ad is successfully loaded
}
func feedAd(_ feedAd: FeedAd, didFailToLoadWithError error: Error) {
// Called when ad loading fails
}
func feedAdDidClick(_ feedAd: FeedAd) {
// Called when the user taps the ad
}
func feedAd(_ feedAd: FeedAd, didTrackImpression impressionData: ImpressionData?) {
// Called when an impression is tracked
}
func feedAdDidUpdateDataSource(_ feedAd: FeedAd) {
// Called when new ads are available — reload collection view
collectionView.reloadData()
}
}
展示广告信息流
要显示广告信息流,请使用 FeedAdCollectionViewAdapter,它为 UICollectionView 提供 dataSource 和 delegate。
在连接适配器之前,通过调用 registerCells(in:) 注册单元格类型。 请确保在设置 dataSource 和 delegate 之前调用此方法。
作为主列表
将适配器的 dataSource 和 delegate 直接链接到 UICollectionView。
final class FeedAdViewController: UIViewController {
private let collectionView = UICollectionView(
frame: .zero,
collectionViewLayout: UICollectionViewFlowLayout()
)
private var feedAdAdapter: FeedAdCollectionViewAdapter?
func setupAdapter() {
guard let feedAd else { return }
let adapter = FeedAdCollectionViewAdapter(feedAd: feedAd)
adapter.registerCells(in: collectionView)
collectionView.dataSource = adapter.dataSource
collectionView.delegate = adapter.delegate
feedAdAdapter = adapter
}
}
与现有内容一起
要将广告信息流与现有内容一起展示,请手动实施 UICollectionViewDataSource。 要执行此操作,请将广告信息流的部分委托给适配器:
final class FeedAdViewController: UIViewController {
private enum Section: Int {
case content
case feed
}
private var contentItems: [ContentItem] = []
private var feedAdAdapter: FeedAdCollectionViewAdapter?
func setupAdapter() {
guard let feedAd else { return }
let adapter = FeedAdCollectionViewAdapter(feedAd: feedAd)
adapter.registerCells(in: collectionView)
feedAdAdapter = adapter
collectionView.dataSource = self
collectionView.delegate = self
}
}
extension FeedAdViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
Section.allCases.count
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
switch Section(rawValue: section) {
case .content:
return contentItems.count
case .feed:
return feedAdAdapter?.dataSource.collectionView(collectionView, numberOfItemsInSection: section) ?? 0
case .none:
return 0
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch Section(rawValue: indexPath.section) {
case .content:
// Return your content cell
return dequeueContentCell(for: indexPath)
case .feed:
return feedAdAdapter?.dataSource.collectionView(collectionView, cellForItemAt: indexPath)
?? UICollectionViewCell()
case .none:
return UICollectionViewCell()
}
}
}
extension FeedAdViewController: UICollectionViewDelegateFlowLayout {
func collectionView(
_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath
) -> CGSize {
switch Section(rawValue: indexPath.section) {
case .feed:
return feedAdAdapter?.delegate.collectionView?(
collectionView,
layout: collectionViewLayout,
sizeForItemAt: indexPath
) ?? .zero
default:
return CGSize(width: collectionView.bounds.width, height: 80)
}
}
}
测试广告信息流集成
使用演示广告单元进行广告测试
使用测试广告来检查您的广告信息流集成和应用本身。
为了确保每个广告请求都返回测试广告,我们创建了一个特殊的演示广告版位 ID,旨在帮助您测试广告的集成情况。
演示 adUnitId: demo-feed-yandex。
重要
在商店中发布您的应用之前,请确保将演示版位 ID 替换为真实 ID。 您可以在 Yandex Advertising Network 界面中获取。
有关所有可用演示广告版位 ID 的列表,请参阅用于测试的演示广告单元。
测试广告集成
您可以使用本机控制台工具测试广告集成。
要查看详细日志,请调用 YandexAds 类的 enableLogging 方法。
YandexAds.enableLogging()
要查看 SDK 日志,请前往控制台工具并设置 Subsystem = com.mobile.ads.ads.sdk。 您还可以按类别和错误级别过滤日志。
如果您在集成广告时遇到问题,您将获得有关问题的详细报告以及如何解决这些问题的建议。
其他资源
- GitHub 链接。