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
的子類別。您可將此物件如同其他任何檢視一樣加入檢視階層。
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
。如果您使用自己的自訂分析報告或中介服務層,可以檢查錯誤代碼值,並偵測此狀況。您可在發生此狀況時以其他廣告聯播網作為後援,但請勿隨後立即重新要求廣告。