現在,在檢視控制器標頭檔(如果您是 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 ...
}