若要善用本指南,請先了解如何建置原生廣告。
現在前往您的檢視控制器標題檔案(若您使用 Swift,則在 Swift 檔案中),匯入 FBAudienceNetwork
並宣告您會遵循 FBAdViewDelegate
通訊協定,然後加入廣告單位的實例變數:
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 | 彈性闊度 | 圖像、圖示、標題、背景資料、說明及呼籲字句按鈕 |
| 400dp | 彈性闊度 | 圖像、圖示、標題、副標題、背景資料、說明及呼籲字句按鈕 |
若要使用附設高度 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 | 彈性闊度 | 圖示、標題、背景資料及呼籲字句按鈕 |
| 120dp | 彈性闊度 | 圖示、標題、背景資料、說明及呼籲字句按鈕 |
若使用原生自訂範本,您就可以自訂下列素材:
如果您想自訂某些素材,建議採用符合您應用程式版面與主題的設計。
您需要組建 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 ...
}