Add Native Banner Ads to an iOS App

The Audience Network allows you to monetize your iOS apps with Facebook ads. This guide explains how to create an iOS app that shows native banner ads. The Native Banner Ad API allows you to build a customized experience for the ads you show in your app. When using the Native Banner Ad API, instead of receiving an ad ready to be displayed, you will receive a group of ad properties such as a title, an image, a call to action, and you will have to use them to construct a custom UIView where the ad is shown.

Let's implement the following native banner ad placement. You will create the following views to our native banner ad.

View #1: ad icon image view

View #2: ad choices view

View #3: ad advertiser name label

View #4: ad sponsored label

View #5: ad call to action button


Step 1: Create Native Banner Ad Views in Storyboard

Step 2: Load and Show Native Banner Ad

Step 3: Verify Impression and Click Logging

Step 4: How to Debug When Ad Not Shown

Step 5: Test Ads Integration

See Known Issues in the Change Log

Step 1: Create Native Banner Ad Views in Storyboard

계속하기 전에 Audience Network 시작하기iOS 시작하기 가이드를 완료했는지 확인하세요.

네이티브 광고 및 배너 광고 제작 시 최적의 사용자 경험을 위해 iOS 레이아웃 가이드라인을 따라야 합니다.

  1. After you have created a new project from iOS Getting Started guides, open Main.storyboard. Add a UIView element to the main View element and name it to adUIView.


  2. In addition, add adIconImageView (FBMediaView), adChoicesView (FBAdChoicesView), adAdvertiserNameLabel (UILabel), adSponsoredLabel (UILabel), adCallToActionButton (UIButton) under adUIView as illustrated in the image below.

  3. You may notice that there is a red arrow nearby View Controller Scene. This usually means that there are missing constraints in your layout.


    You would need to select all the view objects in your scene and click the "resolve layout issue" icon to add missing constraints.



  4. Now that you have created all the UI elements for showing a native banner ad, you will need to reference these UI elements in the ViewController interface. First open the ViewController.m (ViewController.swift if you are a Swift user), then hold down the Control key and drag adUIView inside the ViewController interface object. You can name it as adUIView. After, you will need to do the same thing for adIconImageView , adChoicesView, adAdvertiserNameLabel, adSponsoredLabel, and adCallToActionButton.


  5. Build and run the project. You should see from your device or simulator empty content as follows:

Now that you have created all the UI elements to show native banner ads, the next step is to load the native banner ad and bind the contents to the UI elements.

Step 2: Load and Show Native Banner Ad

  1. Now, in your View Controller header file (or Swift file, if you are a Swift user), import FBAudienceNetwork, declare conformance to the FBNativeBannerAdDelegate protocol, and add an instance variable for the ad unit
    import UIKit
    import FBAudienceNetwork
    
    class ViewController: UIViewController, FBNativeBannerAdDelegate {
      private var nativeBannerAd: FBNativeBannerAd?
    }
    #import <UIKit/UIKit.h>
    @import FBAudienceNetwork;
    
    @interface ViewController : UIViewController <FBNativeBannerAdDelegate>
    
    @property (nonatomic, strong) FBNativeBannerAd *nativeBannerAd;
    
    @end

  2. In viewDidLoad method, add the following lines of code to load native ad content
    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()  
    }
    - (void)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).
      // your code like this to the App Store your users will not receive ads (you will get a ‘No Fill’ error).
      FBNativeBannerAd *nativeBannerAd = [[FBNativeBannerAd alloc] initWithPlacementID:@"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];
    }

    The ID that displays at YOUR_PLACEMENT_ID is a temporary ID for test purposes only.

    If you use this temporary ID in your live code, your users will not receive ads (they will get a No Fill error). You must return here after testing and replace this temporary ID with a live Placement ID.

    To find out how the generate a live Placement ID, refer to Audience Network Setup

  3. The next step is to show ad when content is ready. You would need your View Controller to implement the nativeBannerAdDidLoad method
    func nativeBannerAdDidLoad(_ nativeBannerAd: FBNativeBannerAd) {
        
      if let previousAd = self.nativeBannerAd, previousAd.isAdValid {
        previousAd.unregisterView()
      }
      
      self.nativeBannerAd = nativeBannerAd
      
      adAdvertiserNameLabel.text = nativeBannerAd.advertiserName
      adSponsoredLabel.text = nativeBannerAd.sponsoredTranslation
      
      if let callToAction = nativeBannerAd.callToAction {
        adCallToActionButton.isHidden = false
        adCallToActionButton.setTitle(callToAction, for: .normal)
      } else {
        adCallToActionButton.isHidden = true
      }
      
      // Set native banner ad view tags to declare roles of your views for better analysis in future
      // We will be able to provide you statistics how often these views were clicked by users
      // Views provided by Facebook already have appropriate tag set
      self.adAdvertiserNameLabel.nativeAdViewTag = .title
      self.adCallToActionButton.nativeAdViewTag = .callToAction
      
      // Specify the clickable areas. View you were using to set ad view tags should be clickable.
      let clickableViews: [UIView] = [adCallToActionButton]
      nativeBannerAd.registerView(
        forInteraction: adUIView,
        iconView: adIconImageView,
        viewController: self,
        clickableViews: clickableViews
      )
      
      /*
       // If you don't want to provide native ad view tags you can simply
       // Wire up UIView with the native banner ad; the whole UIView will be clickable.
       nativeBannerAd.registerView(
       forInteraction: adUIView,
       iconView: adIconImageView,
       viewController: self
       )
       */
      
      adChoicesView.corner = .topLeft
      adChoicesView.nativeAd = nativeBannerAd
    }
    - (void)nativeBannerAdDidLoad:(FBNativeBannerAd *)nativeBannerAd
    {
      if (self.nativeBannerAd && self.nativeBannerAd.isAdValid) { 
        [self.nativeBannerAd unregisterView];
      }
      
      self.nativeBannerAd = nativeBannerAd;
    
      // Render native banner ads onto UIView
      self.adAdvertiserNameLabel.text = self.nativeBannerAd.advertiserName;
      self.adSponsoredLabel.text = self.nativeBannerAd.sponsoredTranslation;
     
      if (self.nativeBannerAd.callToAction) {
       [self.adCallToActionButton setHidden:NO];
       [self.adCallToActionButton setTitle:self.nativeBannerAd.callToAction forState:UIControlStateNormal];
      } else {
        [self.adCallToActionButton setHidden:YES];
      }
    
      // Set native banner ad view tags to declare roles of your views for better analysis in future
      // We will be able to provide you statistics how often these views were clicked by users
      // Views provided by Facebook already have appropriate tag set
      self.adAdvertiserNameLabel.nativeAdViewTag = FBNativeAdViewTagTitle;
      self.adCallToActionButton.nativeAdViewTag = FBNativeAdViewTagCallToAction;
    
      // Specify the clickable areas. View you were using to set ad view tags should be clickable.
      NSArray<UIView *> *clickableViews = @[self.adCallToActionButton];
      [nativeBannerAd registerViewForInteraction:self.adUIView
                                        iconView:self.adIconImageView
                                  viewController:self
                                  clickableViews:clickableViews];
    
      // If you don't want to provide native ad view tags you can simply
      // Wire up UIView with the native banner ad; the whole UIView will be clickable.
      // [nativeBannerAd registerViewForInteraction:self.adUIView
      //                                   iconView:self.adIconView
      //                             viewController:self];
    
      self.adChoicesView.corner = UIRectCornerTopLeft;
      self.adChoicesView.nativeAd = nativeBannerAd;
    }
    First, you need to check if there is an existing valid FBNativeBannerAd object. If there is, you will need to unregister it your register the new ad object

    Controlling Clickable Area

    사용자 환경을 개선하고 더 나은 결과를 얻으려면 항상 광고의 클릭 가능한 영역을 제어하여 의도치 않은 클릭을 피해야 합니다. 클릭할 수 없는 여백 정책에 대한 자세한 내용은 Audience Network SDK 정책을 참조하세요.

    What registerViewForInteraction mainly does is register what views will be tappable and what the delegate is to notify when a registered view is tapped. In this case, adCallToActionButton will be tappable and when the view is tapped, ViewController will be notified through FBNativeBannerAdDelegate.

  4. Choose your build target to be device and run the above code, you should see something like this:



시뮬레이터에서 광고를 게재할 때 테스트 광고를 보려면 설정을 테스트 모드로 변경하세요, 자세한 내용은 테스트 모드 사용 방법을 참조하세요.

Step 3: Verify Impression and Click Logging

Optionally, you can add the following functions to handle the cases where the native banner ad is closed or when the user clicks on it:

func nativeBannerAdDidClick(_ nativeBannerAd: FBNativeBannerAd) {
  print("Native banner ad was clicked.")
}

func nativeBannerAdDidFinishHandlingClick(_ nativeBannerAd: FBNativeBannerAd) {
  print("Native banner ad did finish click handling.")
}

func nativeBannerAdWillLogImpression(_ nativeBannerAd: FBNativeBannerAd) {
  print("Native banner ad impression is being captured.")
}
- (void)nativeBannerAdDidClick:(FBNativeBannerAd *)nativeBannerAd
{
    NSLog(@"Native banner ad was clicked.");
}

- (void)nativeBannerAdDidFinishHandlingClick:(FBNativeBannerAd *)nativeBannerAd
{
    NSLog(@"Native banner ad did finish click handling.");
}

- (void)nativeBannerAdWillLogImpression:(FBNativeBannerAd *)nativeBannerAd
{
    NSLog(@"Native banner ad impression is being captured.");
}

Step 4: How to Debug When Ad Not Shown

Add and implement the following function in your View Controller to handle ad loading failures

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

다음 단계

  • 네이티브 광고 사용 방법을 보여주는 코드 샘플을 확인하세요. NativeAdSample은 SDK의 일부로 사용할 수 있으며, FBAudienceNetwork/samples 폴더에서 찾을 수 있습니다. Xcode를 사용하여 프로젝트를 열고 기기나 시뮬레이터에서 실행하세요.

More Resources

Getting Started Guide

Technical guide to get started with the Audience Network

Code Samples

Audience Network Ads Integration Samples

FAQ

Audience Network FAQ

Native Ads Template

A more hands off approach when integrating Native Ads