为有效利用本指南,您应该熟悉如何实现原生广告。
现在,在您的视图控制器标头文件(若您是 Swift 用户,则为 Swift 文件)中,导入 FBAudienceNetwork
,声明该控制器符合 FBNativeAdDelegate
协议,并为该广告单元添加实例变量:
import UIKit
import FBAudienceNetwork
class ViewController: UIViewController, FBNativeAdDelegate {
private var nativeAd: FBNativeAd?
}
然后,在视图控制器的实现文件中添加初始化 FBNativeAd
的方法,并请求加载广告:
override func viewDidLoad() {
super.viewDidLoad()
// Instantiate the ad object.
// NOTE: the placement ID will eventually identify this as your App, you can ignore it for
// now, 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 nativeAd = FBNativeAd(placementID: "YOUR_PLACEMENT_ID")
nativeAd.delegate = self
nativeAd.loadAd()
}
YOUR_PLACEMENT_ID
中显示的编号为临时编号,仅供测试使用。
若您在实时代码中使用此临时编号,您的用户便不会收到广告(他们会收到未填充错误)。您必须在测试之后返回此处,将临时编号换成实时版位编号。
若想了解如何生成实时版位编号,请参阅 Audience Network 设置
现在,您已添加用于加载广告的代码,请添加以下函数,以在广告加载后立即构建广告:
func nativeAdDidLoad(_ nativeAd: FBNativeAd) {
if let previousNativeAd = self.nativeAd, previousNativeAd.isAdValid {
previousNativeAd.unregisterView()
}
self.nativeAd = nativeAd
let adView = FBNativeAdView(nativeAd: nativeAd, with: .genericHeight300)
view.addSubview(adView)
let size = view.bounds.size
let xOffset: CGFloat = size.width / 2 - 160
let yOffset: CGFloat = (size.height > size.width) ? 100 : 20
adView.frame = CGRect(x: xOffset, y: yOffset, width: 320, height: 300)
}
选择设备作为构建目标并运行上述代码后,您应该会看到如下结果:
自定义广告格式有两种模板:
模板视图类型 | 高度 | 宽度 | 包含的属性 |
---|---|---|---|
| 300dp | 宽度不限 | 图像、图标、标题、上下文、描述和行动号召 (CTA) 按钮 |
| 400dp | 宽度不限 | 图像、图标、标题、副标题、上下文、描述和行动号召 (CTA) 按钮 |
如需使用高度选项为 100 和 120 的模板来显示原生横幅广告,您需要创建 FBNativeBannerAd
实例,并在 FBNativeBannerAdView
视图实例中显示该广告,如下所示:
在您的视图控制器标头文件(若您是 Swift 用户,则为 Swift 文件)中导入 FBAudienceNetwork
,声明该控制器符合 FBNativeBannerAdDelegate
协议,并为该广告单元添加实例变量
import UIKit
import FBAudienceNetwork
class ViewController: UIViewController, FBNativeBannerAdDelegate {
private var nativeBannerAd: FBNativeBannerAd?
}
然后,在视图控制器的实现文件中添加初始化 FBNativeBannerAd
的方法,并请求加载广告:
override func viewDidLoad() {
super.viewDidLoad()
// Instantiate a native banner ad object.
// NOTE: the placement ID will eventually identify this as your app. You can ignore it 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 nativeBannerAd = FBNativeBannerAd(placementID: "YOUR_PLACEMENT_ID")
// Set a delegate to get notified when the ad was loaded.
nativeBannerAd.delegate = self
// Initiate a request to load an ad.
nativeBannerAd.loadAd()
}
YOUR_PLACEMENT_ID
中显示的编号为临时编号,仅供测试使用。
若您在实时代码中使用此临时编号,您的用户便不会收到广告(他们会收到未填充错误)。您必须在测试之后返回此处,将临时编号换成实时版位编号。
若想了解如何生成实时版位编号,请参阅 Audience Network 设置
现在,您已添加用于加载广告的代码,请添加以下函数,以在广告加载后立即构建广告:
func nativeBannerAdDidLoad(_ nativeBannerAd: FBNativeBannerAd) {
// 1. If there is an existing valid ad, unregister the view
if let previousAd = self.nativeBannerAd, previousAd.isAdValid {
previousAd.unregisterView()
}
// 2. Retain a reference to the ad object
self.nativeBannerAd = nativeBannerAd
// 3. Instantiate the ad view
let adView = FBNativeBannerAdView(nativeBannerAd: nativeBannerAd, with: .genericHeight100)
view.addSubview(adView)
// 4. Set the frame of the ad view (either manually or using constraints)
let size = view.bounds.size
let xOffset: CGFloat = size.width / 2 - 160
let yOffset: CGFloat = (size.height > size.width) ? 100 : 20
adView.frame = CGRect(x: xOffset, y: yOffset, width: 320, height: 300)
}
自定义广告格式有两种模板:
模板视图类型 | 高度 | 宽度 | 包含的属性 |
---|---|---|---|
| 100dp | 宽度不限 | 图标、标题、上下文和 行动号召 (CTA) 按钮 |
| 120dp | 宽度不限 | 图标、标题、上下文、描述和行动号召 (CTA) 按钮 |
使用原生自定义模板,您可以自定义以下元素:
如需自定义特定元素,建议使用适合您应用布局和主题的设计。
您需要构建 FBNativeAdViewAttributes
对象并提供已加载的原生广告,才能呈现这些元素:
func nativeAdDidLoad(_ nativeAd: FBNativeAd) {
// Instantiate the attributes to customize the view
let attributes = FBNativeAdViewAttributes()
attributes.backgroundColor = UIColor(white: 0.9, alpha: 1)
attributes.buttonColor = UIColor(red: 66 / 255.0, green: 108 / 255.0, blue: 173 / 255.0, alpha: 1)
attributes.buttonTitleColor = .white
// Feed the attributes to the view
let adView = FBNativeAdView(nativeAd: nativeAd, with: .genericHeight300, with: attributes)
... Rest of implementation ...
}
上述代码呈现的广告如下:
func nativeBannerAdDidLoad(_ nativeBannerAd: FBNativeBannerAd) {
// Instantiate the attributes to customize the view
let attributes = FBNativeAdViewAttributes()
attributes.backgroundColor = UIColor(white: 0.9, alpha: 1)
attributes.buttonColor = UIColor(red: 66 / 255.0, green: 108 / 255.0, blue: 173 / 255.0, alpha: 1)
attributes.buttonTitleColor = .white
// Instantiate the view and feed the attributes to the initializer
let adView = FBNativeBannerAdView(nativeBannerAd: nativeBannerAd, with: .genericHeight100, with: attributes)
... Rest of implementation ...
}