Implementing Facebook Audien Network Ads for iOS Apps in Swift

865 views Asked by At

There are only guide for implement Facebook Audien Network ads for IOS app in Obj C: https://developers.facebook.com/docs/audience-network/get-started/ios

How can I get document for Swift? Thanks!

1

There are 1 answers

0
Blue_Alien On

Step 1: Add Facebook Audience Network

pod 'FBAudienceNetwork'

Step 2: import FBAudienceNetwork

import FBAudienceNetwork

Step 2: Add Delegate to your ViewController

class FBInterstitialDemo: UIViewController, FBInterstitialAdDelegate {
}

Step 3: Create FBInterstitialAd instance

let interstitialFBAD: FBInterstitialAd = FBInterstitialAd(placementID: "282907702901663_391523248706774")

Step 4: set delegate for FBInterstitialAd

interstitialFBAD.delegate = self;

Step 5: Load Ad

interstitialFBAD.load()

Step 6: Show Ad, if the Ad is loaded && isValid

if interstitialAd.isAdValid {
            interstitialAd.show(fromRootViewController: self)
        }

Here is a demo ViewController for reference:

import UIKit
import FBAudienceNetwork

let interstitialFBAD: FBInterstitialAd = FBInterstitialAd(placementID: "your_placement_id_here")

class FBInterstitialDemo: UIViewController, FBInterstitialAdDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        interstitialFBAD.delegate = self;
        interstitialFBAD.load()
        
        // Do any additional setup after loading the view.
    }
    
    func interstitialAdDidLoad(_ interstitialAd: FBInterstitialAd) {
        if interstitialAd.isAdValid {
            interstitialAd.show(fromRootViewController: self)
        }
            
        }
        
        func interstitialAd(_ interstitialAd: FBInterstitialAd, didFailWithError error: Error) {
            print(error)
        }
        
        func interstitialAdDidClick(_ interstitialAd: FBInterstitialAd) {
            print("Did tap on ad")
        }


        func interstitialAdDidClose(_ interstitialAd: FBInterstitialAd) {
            print("Did close ad")
        }
    

}

posting after so long, because i could not find a tutorial in swift to integrate Facebook Audience Network