Home
Unlabelled
Adding Native Banner Ad to your iOS app
Adding Native Banner Ad to your 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 viewView #2: ad choices viewView #3: ad advertiser name label | View #4: ad sponsored labelView #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
Ensure you have completed the Audience Network Getting Started and iOS Getting Started guides before you proceed.
When designing native ads and banner ads, ensure you have followed iOS layout guideline for optimal user experience.
- 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 toadUIView
. - In addition, add
adIconImageView
(FBAdIconView),adChoicesView
(FBAdChoicesView),adAdvertiserNameLabel
(UILabel),adSponsoredLabel
(UILabel),adCallToActionButton
(UIButton) underadUIView
as illustrated in the image below. - 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. - 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
, then hold down the Control key and dragadUIView
inside the ViewController interface object. You can name it asadUIView
. After, you will need to do the same thing foradIconImageView
,adChoicesView
,adAdvertiserNameLabel
,adSponsoredLabel
, andadCallToActionButton
. - 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
- Now, in your View Controller header file, import the SDK header, declare that ViewController implements the
FBNativeBannerAdDelegate
protocol and add anativeBannerAd
instance variable: #import "ViewController.h" @import FBAudienceNetwork; @interface ViewController () <FBNativeBannerAdDelegate> @property (nonatomic, strong) FBNativeBannerAd *nativeBannerAd; @end
- In
viewDidLoad
method, add the following lines of code to load native ad content.- (void)viewDidLoad { [super viewDidLoad]; // Instantiate a NativeBannerAd 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). 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]; }
ReplaceYOUR_PLACEMENT_ID
with your own placement id string. If you don't have a placement id or don't know how to get one, you can refer to the Getting Started Guide - The next step is to show ad when content is ready. You would need to implement
nativeBannerAdDidLoad
method inViewController.m
file.- (void)nativeBannerAdDidLoad:(FBNativeBannerAd *)nativeBannerAd { self.nativeBannerAd = nativeBannerAd; [self showNativeBannerAd]; } - (void)showNativeBannerAd { if (self.nativeBannerAd && self.nativeBannerAd.isAdValid) { [self.nativeBannerAd unregisterView]; // 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]; // In 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 will need to check if there is an existingnativeBannerAd
object. If there is, you will need to callunregisterView
method. Then you will call[nativeBannerAd registerViewForInteraction:iconView:viewController:clickableViews:]
method.Controlling Clickable Area
For a better user experience and better results, you should always consider controlling the clickable area of your ad to avoid unintentional clicks. Please refer to Audience Network SDK Policy page for more details about white space unclickable enforcement.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 throughFBNativeBannerAdDelegate
. - Choose your build target to be device and run the above code, you should see something like this:
When running ads in the simulator, change the setting to test mode to view test ads. Please go to How to Use Test Mode for more information.
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:
- (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 implementation file to handle ad loading failures and completions:
- (void)nativeBannerAd:(FBNativeBannerAd *)nativeBannerAd didFailWithError:(NSError *)error { NSLog(@"Native banner ad failed to load with error: %@", error); }
Next Steps
- Follow our guides for integrating different Ad Formats in your app:
- Native Ads
- Interstitial Ads
- Banners
- Test ads integration with your app
- Submit your app for review.
- As soon as we receive a request for an ad from your app or website, we'll review it to make sure it complies with Audience Network policies and the Facebook community standards. Learn more about our review process.
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
|
Adding Native Banner Ad to your iOS app
Reviewed by Code Labe
on
August 27, 2019
Rating: 5