通过 Audience Network,您可以借助 Facebook 广告让 iOS 应用实现变现。本指南介绍如何创建显示横幅广告和中矩形广告的 iOS 应用。
You can change placements in Monetization Manager to the Medium Rectangle format if these were previously configured as Banner for bidding. Similarly, for any new medium rectangle placements, navigate to the placement settings page in Monetization Manager and select Medium Rectangle (not Banner).
Placements will deliver as normal even if they are not changed to the medium rectangle format. However, to avoid confusion, we recommend that you change these placements to medium rectangle.
如果您有意了解其他类型的广告单元,请查看可用的广告单元类型清单。
让我们实现以下横幅广告版位。
请务必先阅读完 iOS 设置指南,然后再继续后续步骤。
设计原生广告和横幅广告时,请务必遵守 iOS 布局指南,以便打造最佳的用户体验。
adContainer
。FBAudienceNetwork
,声明该控制器符合 FBAdViewDelegate
协议,并为该广告单元添加实例变量
import UIKit
import FBAudienceNetwork
class ViewController: UIViewController, FBAdViewDelegate {
@IBOutlet private var adContainer: UIView!
private var adView: FBAdView?
}
viewDidLoad
;新建 FBAdView
的实例,然后将该实例添加到视图中。FBAdView
是 UIView
的子类。您可以将 FBAdView 添加到视图层次结构中,方法与任何其他视图一样。
override func viewDidLoad() {
super.viewDidLoad()
// Instantiate an AdView object.
// NOTE: the placement ID will eventually identify this as your app, you can ignore while you
// are testing and replace it later when you have signed up.
// While you are using this temporary code you will only get test ads and if you release
// your code like this to the App Store your users will not receive ads (you will get a 'No Fill' error).
let adView = FBAdView(placementID: "YOUR_PLACEMENT_ID", adSize: kFBAdSizeHeight50Banner, rootViewController: self)
adView.frame = CGRect(x: 0, y: 0, width: 320, height: 250)
adView.delegate = self
adView.loadAd()
self.adView = adView
}
FBAdView
的 adSize 参数中提供 kFBAdSizeHeight250Rectangle
即可。FBAdView
中使用三种广告尺寸。广告单元的宽度可以调整,最小为 320 像素,只有高度已明确规定。广告格式 | AdSize 参考 | 尺寸 | 建议 |
---|---|---|---|
中矩形横幅广告 |
| 300x250 | 强烈推荐此格式,因为此格式具备更出色的效果、更高的质量和更高的 CPU 效率 |
标准横幅广告 |
| 320x50 | 这种格式适用于手机,但不推荐使用,因为效果和质量较差 |
大型横幅广告 |
| 320x90 | 这种格式适用于平板电脑大屏设备,但不推荐使用,因为效果和质量较差 |
YOUR_PLACEMENT_ID
。如果没有版位编号或不知道如何获取版位编号,请参阅入门指南。选择设备作为构建目标并运行上述代码后,您应该会看到如下所示的结果:在模拟器中投放广告时,应将设置更改为测试模式,以查看测试广告。详情请参阅如何使用测试模式。
您可以选择添加以下函数,用于处理广告关闭或用户点击广告的情况:
func adViewDidClick(_ adView: FBAdView) {
print("Ad was clicked.")
}
func adViewDidFinishHandlingClick(_ adView: FBAdView) {
print("Ad did finish click handling.")
}
func adViewWillLogImpression(_ adView: FBAdView) {
print("Ad impression is being captured.")
}
在视图控制器中添加并实现以下两个委托函数,以处理广告加载失败的情况:
func adView(_ adView: FBAdView, didFailWithError error: Error) {
print("Ad failed to load with error: \(error.localizedDescription)")
}
func adViewDidLoad(_ adView: FBAdView) {
print("Ad was loaded and ready to be displayed")
showAd()
}
private func showAd() {
guard let adView = adView, adView.isAdValid else {
return
}
adContainer.addSubview(adView)
}
当未显示任何广告时,系统将调用 adView:didFailWithError:
,其中 error.code
会设置为 1001
。如果您使用自己的自定义报告或中介层,则可能需要检查代码值并检测这一情况。在这种情况下,您可回退至另一个广告网络,但不能立即重新请求广告。