在 iOS 應用程式中加入插頁廣告

Audience Network 可讓您運用 Facebook 廣告,將自己的 iOS 應用程式變成營利來源。插頁廣告是可在應用程式中顯示的全螢幕廣告。請根據本指南顯示此類廣告單位。或者,若您對其他類型的廣告單位感興趣,請參閱可用類型清單

我們來一起執行以下插頁廣告版位。



步驟 1:載入並顯示插頁廣告檢視畫面

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

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

第 4 步:測試廣告整合

步驟 1:載入並顯示插頁廣告檢視畫面

確保您在開始操作前,已經先行參閱 Audience Network 新手指南iOS 新手指南

  1. 在您根據 iOS 新手指南建立新專案後,上載 FBAudienceNetwork、宣告 ViewController 有執行 FBInterstitialAdDelegate 協定,並且為插頁廣告單位加入實例變數。
    import UIKit
    import FBAudienceNetwork
    
    class ViewController: UIViewController, FBInterstitialAdDelegate {
      private var interstitialAd: FBInterstitialAd?
    }
    #import <UIKit/UIKit.h>
    @import FBAudienceNetwork;
    
    @interface ViewController : UIViewController <FBInterstitialAdDelegate>
    @property (nonatomic, strong) FBInterstitialAd *interstitialAd;
    @end

  2. 接下來,在檢視畫面控制器的 viewDidLoad 方法中將廣告物件實例化,並且執行 interstitialAdDidLoad
    override func viewDidLoad() {
      super.viewDidLoad()
    
      // Instantiate an InterstitialAd 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 interstitialAd = FBInterstitialAd(placementID: "YOUR_PLACEMENT_ID")
      interstitialAd.delegate = self
    
      // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
      interstitialAd.load()
      
      self.interstitialAd = interstitialAd
    }
    
    func interstitialAdDidLoad(_ interstitialAd: FBInterstitialAd) {
      guard interstitialAd.isAdValid else {
        return
      }
      print("Ad is loaded and ready to be displayed")
      interstitialAd.show(fromRootViewController: self)
    }
    - (void)viewDidLoad 
    {
      [super viewDidLoad];
    
      // Instantiate an InterstitialAd 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).
      self.interstitialAd = [[FBInterstitialAd alloc] initWithPlacementID:@"YOUR_PLACEMENT_ID"];
    
      self.interstitialAd.delegate = self;
    
      // For auto play video ads, it's recommended to load the ad at least 30 seconds before it is shown
      [self.interstitialAd loadAd];
    }
    
    - (void)interstitialAdDidLoad:(FBInterstitialAd *)interstitialAd
    {
      NSLog(@"Ad is loaded and ready to be displayed");
    
      if (interstitialAd && interstitialAd.isAdValid) {
        // You can now display the full screen ad using this code:
        [interstitialAd showAdFromRootViewController:self];
      }
    }

  3. YOUR_PLACEMENT_ID 顯示的編號為臨時編號,僅作測試之用。

    若您在實時代碼中使用此臨時編號,用戶將不會收到廣告(而是收到未供應廣告錯誤)。您必須在測試後返回此處,並將此臨時編號替換為實時版位編號。

    請參考 Audience Network 設定,了解產生實時版位編號的方法。

    選擇目標組建作為裝置,然後執行上方的程式碼,您應該就會看到類似以下的內容:

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

當廣告顯示在畫面上時,請勿對 FBInterstitialAd 呼叫 loadAd。如果您需要載入另一個 FBInterstitialAd 供日後使用,可在用戶關閉目前的 FBInterstitialAd 後載入,例如在 interstitialAdDidClose 回呼中載入。

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

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

func interstitialAdWillLogImpression(_ interstitialAd: FBInterstitialAd) {
  print("The user sees the ad")
  // Use this function as indication for a user's impression on the ad.
}

func interstitialAdDidClick(_ interstitialAd: FBInterstitialAd) {
  print("The user clicked on the ad and will be taken to its destination")
  // Use this function as indication for a user's click on the ad.
}

func interstitialAdWillClose(_ interstitialAd: FBInterstitialAd) {
  print("The user clicked on the close button, the ad is just about to close")
  // Consider to add code here to resume your app's flow
}

func interstitialAdDidClose(_ interstitialAd: FBInterstitialAd) {
  print("The user clicked on the close button, the ad is just about to close")
  // Consider to add code here to resume your app's flow
}
- (void)interstitialAdWillLogImpression:(FBInterstitialAd *)interstitialAd 
{
  NSLog(@"The user sees the ad");
  // Use this function as indication for a user's impression on the ad.
}

- (void)interstitialAdDidClick:(FBInterstitialAd *)interstitialAd
{
  NSLog(@"The user clicked on the ad and will be taken to its destination");
  // Use this function as indication for a user's click on the ad.
}

- (void)interstitialAdWillClose:(FBInterstitialAd *)interstitialAd
{
  NSLog(@"The user clicked on the close button, the ad is just about to close");
  // Consider to add code here to resume your app's flow
}

- (void)interstitialAdDidClose:(FBInterstitialAd *)interstitialAd
{
  NSLog(@"Interstitial had been closed");
  // Consider to add code here to resume your app's flow
}

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

在您的檢視畫面控制器執行檔案中新增並執行下列函數,以處理廣告載入錯誤。

func interstitialAd(_ interstitialAd: FBInterstitialAd, didFailWithError error: Error) {
  print("Interstitial ad failed to load with error: \(error.localizedDescription)")
}
- (void)interstitialAd:(FBInterstitialAd *)interstitialAd didFailWithError:(NSError *)error
{
  NSLog(@"Interstitial ad failed to load with error: %@", error);
}

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

後續步驟

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