Edit

Get Facebook demand for native on iOS

This article describes how to use the iOS SDK Facebook adapter to retrieve and display native ads from Meta Audience Network.

Note

The Facebook adapter requires iOS 15.0 or later.

SDK installation

Install the Facebook CSR adapter by using Swift Package Manager. The adapter package includes compatible versions of the iOS SDK and Meta Audience Network SDK as dependencies.

  1. Open your project in Xcode.

  2. Select the project in the Project navigator, and then select Package Dependencies.

  3. Select the + button to add a package dependency.

  4. Enter the following package URL in the search box, and then press Return:

    https://github.com/appnexus/mobile-sdk-ios-mediation-facebook
    
  5. Choose the version setting that fits your project. For new projects, select Up to Next Major Version. Then select Add Package.

  6. Select the ANFacebookCSRAdapter product and your app target, and then select Add Package.

  7. Verify that ANFacebookCSRAdapter appears under Package Dependencies.

Initialize Facebook's Audience Network SDK

Early in your app lifecycle, initialize Meta Audience Network and pass the result to ANFBSettings.

FBAudienceNetworkAds.initialize(with: nil) { results in
    ANFBSettings.setFBAudienceNetworkInitialize(results.isSuccess) // Store the initialization result
}

When initialization succeeds, the adapter can retrieve the Meta bidder token. Otherwise, getBidderToken returns nil.

Set advertiser tracking according to the user's tracking choice before requesting ads. Replace the example value with the result from your consent flow.

let userAllowedTracking = true
FBAdSettings.setAdvertiserTrackingEnabled(userAllowedTracking) // Set the user's tracking choice

Request and render a native ad

Note

Retain the request and response while the ad is loading or displayed. Releasing the response unregisters its view.

The following example assumes that your view controller has a native ad container with icon and call-to-action outlets. It registers the call-to-action control as the clickable view.

final class FacebookNativeAdViewController: UIViewController, ANNativeAdRequestDelegate {
    @IBOutlet private weak var nativeAdView: UIView!
    @IBOutlet private weak var iconImageView: UIImageView!
    @IBOutlet private weak var callToActionButton: UIButton!

    private var nativeAdRequest: ANNativeAdRequest?
    private var nativeAdResponse: ANNativeAdResponse?

    override func viewDidLoad() {
        super.viewDidLoad()

        let request = ANNativeAdRequest()
        request.placementId = "18793423" // Set placement ID
        request.delegate = self // Set the request delegate
        nativeAdRequest = request
        request.loadAd() // Load the ad
    }

    func adRequest(_ request: ANNativeAdRequest, didReceive response: ANNativeAdResponse) {
        nativeAdResponse = response
        iconImageView.image = response.iconImage
        callToActionButton.setTitle(response.callToAction, for: .normal) // Render the call-to-action text

        if let facebookAdapter = response.customElements?[kANNativeCSRObject]
            as? ANAdAdapterCSRNativeBannerFacebook {
            facebookAdapter.registerView(
                forTracking: nativeAdView,
                withRootViewController: self,
                iconImageView: iconImageView,
                clickableViews: [callToActionButton]
            ) // Register the Facebook response
        } else {
            do {
                try response.registerView(
                    forTracking: nativeAdView,
                    withRootViewController: self,
                    clickableViews: [callToActionButton]
                ) // Register other native responses
            } catch {
                print("Unable to register the native ad view: \(error)")
            }
        }
    }

    func adRequest(
        _ request: ANNativeAdRequest,
        didFailToLoadWithError error: Error,
        with adResponseInfo: ANAdResponseInfo?
    ) {
        print("Native ad failed to load: \(error.localizedDescription)")
    }
}

The Facebook adapter provides registration overloads for these combinations:

Icon view Clickable views
FBMediaView Entire native ad view
FBMediaView Selected subviews
UIImageView Entire native ad view
UIImageView Selected subviews