在 iOS 應用程式加入橫額廣告和中型長方形廣告

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.

若您對其他類型的廣告單位感興趣,請參閱可用類型清單

橫額廣告和中型長方形廣告執行步驟

我們將會執行以下橫額廣告版位。



第 1 步:載入並顯示廣告檢視

第 2 步:驗證展示及點擊記錄

第 3 步:廣告無法顯示時的除錯方式

第 4 步:測試廣告整合

第 1 步:載入並顯示廣告檢視

確保您在開始操作前,已先行參閱 iOS 設定指南

在設計原生廣告和橫額廣告時,請務必遵循 iOS 版面配置指南,以提供用戶最佳體驗。

  1. 利用 iOS 新手指南建立新的專案後,請開啟 Main.storyboard。在主要的檢視元素中加入 UIView 元素,並將其命名為 adContainer
  2. 現在前往您的檢視控制器標題檔案(若您使用 Swift,則在 Swift 檔案中),匯入 FBAudienceNetwork 並宣告您會遵循 FBAdViewDelegate 通訊協定,然後加入廣告單位的實例變數。
    import UIKit
    import FBAudienceNetwork
    
    class ViewController: UIViewController, FBAdViewDelegate {
    
      @IBOutlet private var adContainer: UIView!
    
      private var adView: FBAdView?
    }
    #import <UIKit/UIKit.h>
    @import FBAudienceNetwork;
    
    @interface ViewController : UIViewController <FBAdViewDelegate>
    
    @property (nonatomic, weak) IBOutlet UIView *adContainer;
    @property (nonatomic, strong) FBAdView *adView;
    
    @end

  3. 將下方程式碼加入 viewDidLoad;建立 FBAdView 的新實例,並將其加入檢視。FBAdViewUIView 的子類別。如同其他任何檢視一樣,您可將其加入檢視層級。
    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
    }
    - (void)viewDidLoad
    {
      [super viewDidLoad];
      // Instantiate an AdView 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).
      self.adView = [[FBAdView alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID" adSize:kFBAdSizeHeight50Banner rootViewController:self];
      self.adView.frame = CGRectMake(0, 0, 320, 250);
      self.adView.delegate = self;
      [self.adView loadAd];
    }
    若要改為加入中型長方形廣告,只需在 FBAdView 的 adSize 參數中提供 kFBAdSizeHeight250Rectangle 即可。

    Audience Network 支援在 FBAdView 中使用三種廣告尺寸。廣告單位的寬度可彈性調整,但最少須為 320 像素;高度是唯一已定義的參數。

    廣告格式 AdSize 參考 大小 建議

    中型長方形

    kFBAdSizeHeight 250Rectangle

    300x250

    我們強烈建議使用此格式,因為它能提高成效、品質和 CPU 效能

    標準橫額廣告

    kFBAdSizeHeight 50Banner

    320x50

    此格式適用於手機,但成效和品質欠佳,因此不建議使用

    大型橫額廣告

    kFBAdSizeHeight 90Banner

    320x90

    此格式適用於平板電腦和大型裝置,但成效和品質欠佳,因此不建議使用

  4. 以您自己的版位編號字串取代 YOUR_PLACEMENT_ID。如果您沒有版位編號或不知道該如何取得此編號,請參考新手指南。選擇建立目標為裝置,並執行上述程式碼,您就會看到類似下方的內容:



在模擬器中刊登廣告時,請變更設定至測試模式以查看測試版廣告。請參閱如何使用測試模式以瞭解詳情。

第 2 步:驗證展示及點擊記錄

您也可以選擇新增以下函數,以處理廣告關閉或用戶點擊廣告的情況:

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.")
}
- (void)adViewDidClick:(FBAdView *)adView
{
  NSLog(@"Ad was clicked.");
}

- (void)adViewDidFinishHandlingClick:(FBAdView *)adView
{
  NSLog(@"Ad did finish click handling.");
}

- (void)adViewWillLogImpression:(FBAdView *)adView
{
  NSLog(@"Ad impression is being captured.");
}

第 3 步:廣告無法顯示時的除錯方式

在您的檢視控制器中新增並執行下列兩個委派函數,以處理廣告載入錯誤:

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)
}
- (void)adView:(FBAdView *)adView didFailWithError:(NSError *)error
{
  NSLog(@"Ad failed to load with error: %@", error);
}

- (void)adViewDidLoad:(FBAdView *)adView
{
  NSLog(@"Ad was loaded and ready to be displayed");
  [self showAd];
}

- (void)showAd
{
  if (self.adView && self.adView.isAdValid) {
    [self.adContainer addSubview:self.adView];
  }
}

若無廣告可顯示,系統會呼叫 adView:didFailWithError: 並將 error.code 設為 1001。如您使用自己自訂的回報或中介層,最好檢查錯誤代碼值以偵測此狀況。您可在發生此狀況時以其他廣告聯盟作為遞補,但請勿在之後立即重新要求廣告。


後續步驟

  • 探索程式碼範例,瞭解如何使用原生廣告。SDK 在 NativeAdSample 資料夾中提供了 FBAudienceNetwork/samples。以 Xcode 開啟專案,然後在裝置或模擬器上執行該專案。