在 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。在主要 View 元素新增 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 在 FBAudienceNetwork/samples 資料夾中提供了 NativeAdSample。使用 Xcode 開啟該專案,然後在裝置或模擬器上執行。