import SwiftUI import AppTrackingTransparency import UIKit import AppLovinSDK import FirebaseAnalyticsSwift import FirebaseAnalytics 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: “”) 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) { 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 { // FAN_PLACEMENT_ID is in the following format “XXXXXXXXXXXXX_YYYYYYYYYYYY” ad.show(forPlacement: ““, 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() // This will be the place to enable access to premium content, e.g. single lesson / flashcard deck 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 } } }