AppLovin / AppLovin-MAX-SDK-iOS

84 stars 69 forks source link

Ads from FAM are not showing #283

Open jkozlowicz opened 4 months ago

jkozlowicz commented 4 months ago

MAX SDK Version

12.1.0

Device/Platform Info

iPhone XR, iOS 16.3.1

Current Behavior

I am trying to load ads from Facebook Audience Network and it is not working at all. I cannot get any more details apart from the fact that the MAInterstitialAd.isReady always returns false. However, I can load ads from AppLovin's network just fine.

Expected Behavior

I am able to select Facebook as the live network and load an ad using it.

How to Reproduce

Below is the code I am using to load and display the ads. I use SwiftUI so I had to do some extra adjustments with the view controllers. The code has been basically taken from AppLovin's integration guide for iOS and adapted to SwiftUI's needs:

import SwiftUI
import AppTrackingTransparency
import UIKit
import AppLovinSDK

class AppLovingInterstitialAdLoader: NSObject {
    var interstitialAd: MAInterstitialAd!

    //Want to have one instance of the ad for the entire app
    //We can do this b/c you will never show more than 1 ad at once so only 1 ad needs to be loaded
    static let shared = AppLovingInterstitialAdLoader()

    func loadAd() {
        self.interstitialAd = MAInterstitialAd(adUnitIdentifier: "MY-APPLOVING-AD-UNIT-ID")
        self.interstitialAd.load()
    }
}

struct AppLovinInterstitialView: UIViewControllerRepresentable {

    //Here's the Ad Object we just created
    let interstitialAd: MAInterstitialAd! = AppLovingInterstitialAdLoader.shared.interstitialAd
    var retryAttempt = 0.0
    @Binding var isPresented: Bool

    init(isPresented: Binding<Bool>) {
        self._isPresented = isPresented
    }

    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }

    //Make's a SwiftUI View from a UIViewController
    func makeUIViewController(context: Context) -> UIViewController {
        let view = UIViewController()

        //Show the ad after a slight delay to ensure the ad is loaded and ready to present
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(1)) {
            self.showAd(from: view)
        }

        return view
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {

    }

    //Presents the ad if it can, otherwise dismisses so the user's experience is not interrupted
    func showAd(from root: UIViewController) {

        if let ad = interstitialAd, ad.isReady {
            // MY FAM PLACEMENT_ID IS HERE. IT IS A VALID, COPIED PLACEMENT_ID FROM FB MONETISATION MANAGER.
            // SHOULD I BE PASSING HERE SOMETHING ELSE OR DIFFERENTLY?
            ad.show(forPlacement: "XXX_YYY", customData: nil, viewController: root)
        } else {
            print("Ad not ready")
            self.isPresented.toggle()
        }
    }

    class Coordinator: NSObject, MAAdDelegate {
        var parent: AppLovinInterstitialView

        init(_ parent: AppLovinInterstitialView) {
            self.parent = parent
            super.init()
            self.parent.interstitialAd?.delegate = self
        }

        func didFailToLoadAd(forAdUnitIdentifier adUnitIdentifier: String, withError error: MAError) {
            // Interstitial ad failed to load
            // We recommend retrying with exponentially higher delays up to a maximum delay (in this case 64 seconds)

            self.parent.retryAttempt += 1
            let delaySec = pow(2.0, min(6.0, self.parent.retryAttempt))

            DispatchQueue.main.asyncAfter(deadline: .now() + delaySec) {
                AppLovingInterstitialAdLoader.shared.loadAd()
            }
            print("didFailToLoadAd")
        }

        func didDisplay(_ ad: MAAd) {
            // Pause your app's background audio
            print("Ad displayed")
        }

        func didClick(_ ad: MAAd) {
            print("Ad clicked")
        }

        func didHide(_ ad: MAAd) {
            // Resume your app's background audio
            // Interstitial ad is hidden. Pre-load the next ad
            AppLovingInterstitialAdLoader.shared.loadAd()

            print("Ad hidden")
        }

        func didFail(toDisplay ad: MAAd, withError error: MAError) {
            // Interstitial ad failed to display. We recommend loading the next ad
            AppLovingInterstitialAdLoader.shared.loadAd()
            print("didFail")
        }

        func didLoad(_ ad: MAAd) {
            // Interstitial ad is ready to be shown. 'interstitialAd.isReady' will now return 'true'

            // Reset retry attempt
            self.parent.retryAttempt = 0
        }

    }
}

Additional Info


    private func initializeAppLovin() {
        // MARK: AppLoving Stuff
        // Please make sure to set the mediation provider value to "max" to ensure proper functionality

        let settings = ALSdkSettings()
        settings.termsAndPrivacyPolicyFlowSettings.isEnabled = true
        settings.termsAndPrivacyPolicyFlowSettings.privacyPolicyURL = URL(string: "https://xyz.com/privacy")
        settings.termsAndPrivacyPolicyFlowSettings.termsOfServiceURL = URL(string: "https://xyz.com/terms")

        ALSdk.shared()!.mediationProvider = "max"
        let userIdForAppLovin = getUserId()
        ALSdk.shared()!.userIdentifier = userIdForAppLovin
        ALSdk.shared()!.initializeSdk { (configuration: ALSdkConfiguration) in
            // Start loading ads
        }
        // MARK: AppLoving Stuff
    }

    func application(
        _ application: UIApplication,
        didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
    ) -> Bool {
      ....
        FBAdSettings.setDataProcessingOptions([])
        FBAdSettings.setAdvertiserTrackingEnabled(true)
        FBAudienceNetworkAds.initialize(with: nil, completionHandler: {(FBAdInitResults) -> Void in
            self.initializeAppLovin()
        })
      ....
     }
Screenshot 2024-01-01 at 21 22 50 Screenshot 2024-01-01 at 21 24 39 Screenshot 2024-01-01 at 21 20 40 Screenshot 2024-01-01 at 21 20 03