The Audience Network allows you to monetize your iOS apps with Facebook ads. This guide explains how to create an iOS app that shows banner and medium rectangle ads.
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.
If you're interested in other kinds of ad units, see the list of available types.
Let's implement the following banner ad placement.
Ensure you have completed the iOS Setup Guides guide before you proceed.
When designing native ads and banner ads, ensure you have followed iOS layout guideline for optimal user experience.
adContainer
. FBAudienceNetwork
, declare conformance to the FBAdViewDelegate
protocol, and add an instance variable for the ad unit
import UIKit
import FBAudienceNetwork
class ViewController: UIViewController, FBAdViewDelegate {
@IBOutlet private var adContainer: UIView!
private var adView: FBAdView?
}
viewDidLoad
; Create a new instance of FBAdView
and add it to the view. FBAdView
is a subclass of UIView
. You can add it to your view hierarchy just like any other view.
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
}
kFBAdSizeHeight250Rectangle
in the adSize parameter to FBAdView
.
FBAdView
. A unit's width is flexible with a minimum of 320px, and only the height is defined. Ad Format | AdSize Reference | Size | Recommendation |
---|---|---|---|
Medium Rectangle |
| 300x250 | This format is highly recommended because it provides higher performance, higher quality, and more CPU efficient |
Standard Banner |
| 320x50 | This format is suited to phones but not recommended because of poor performance and quality |
Large Banner |
| 320x90 | This format is suited to tablets and larger devices but not recommended because of poor performance and quality |
YOUR_PLACEMENT_ID
with your own placement id string. If you don't have a placement id or don't know how to get one, refer to the Getting Started Guide.
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.
Optionally, you can add the following functions to handle the cases where the ad is closed or when the user clicks on it:
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.")
}
Add and implement the following two delegate functions in your View Controller to handle ad loading failures:
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)
}
When there is no ad to show, the adView:didFailWithError:
will be called with error.code
set to 1001
. If you use your own custom reporting or mediation layer, you may want to check the code value and detect this case. You can fallback to another ad network in this case, but do not re-request an ad immediately after.
Relevant code samples in both Swift and Objective C are available on our GitHub sample app repository
Test your ads integration with your app
Submit your app for review