Show AdMob Interstitial Ad Before Sheet Appears in SwiftUI

83 Views Asked by At

I'm trying to integrate AdMob interstitial ads into my app but can figure this out. Clicking Submit shows the ad just fine but the sheet doesn't appear after dismissing the ad.

Here's my code:

class AppVariables: ObservableObject {
    static let shared = AppVariables()
    public var isProd = Bundle.main.object(forInfoDictionaryKey: "IsProduction") as? Bool
    @Published var showingResult: Bool = false
    @Published var showingConstitutionalCriteria: Bool = false
    @Published var isLoading: Bool = false
}
import GoogleMobileAds
import SwiftUI

// MARK: - Helper to present Interstitial Ad
struct AdViewControllerRepresentable: UIViewControllerRepresentable {
    let viewController = UIViewController()
    
    func makeUIViewController(context: Context) -> some UIViewController {
        return viewController
    }
    
    func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
}

class InterstitialAdCoordinator: NSObject, GADFullScreenContentDelegate {
    var interstitial: GADInterstitialAd?
    private let isProd = Bundle.main.object(forInfoDictionaryKey: "IsProduction") as? Bool
    private let appVariables: AppVariables = AppVariables()
    
    
    func loadAd() {
        let request = GADRequest()
        GADInterstitialAd.load(
            withAdUnitID: isProd! ? appVariables.interstitialAdId : appVariables.interstitialTestAdId, request: request) { ad, error in
            self.interstitial = ad
            self.interstitial?.fullScreenContentDelegate = self
        }
    }
    
    func adDidDismissFullScreenContent(_ ad: GADFullScreenPresentingAd) {
        AppVariables.shared.showingResult.toggle()
        print(AppVariables.shared.showingResult)
        self.loadAd()
    }
    
    func showAd(from viewController: UIViewController) {
        guard let interstitial = interstitial else {
            return print("Ad wasn't ready")
        }
        
        interstitial.present(fromRootViewController: viewController)
    }
}

extension NSNotification.Name {
    static let onAdDidDismiss = Notification.Name("onAdDidDismiss")
}
import SwiftUI

struct SymptomView: View {
    @EnvironmentObject var appVariables: AppVariables
    private let adCoordinator = InterstitialAdCoordinator()
    private let adViewControllerRepresentable = AdViewControllerRepresentable()

    @State var path: String = "/symptoms"
    
    var body: some View {
        Form {...}
            HStack {
                Spacer()
                Button("Submit") {
                    adCoordinator.showAd(from: adViewControllerRepresentable.viewController)
                }
                .buttonStyle(.borderedProminent)
                .buttonBorderShape(.capsule)
                Spacer()
            }
        }
        .onAppear {
            adCoordinator.loadAd()
        }
        .background {
            // Add the adViewControllerRepresentable to the background so it
            // doesn't influence the placement of other views in the view hierarchy.
            adViewControllerRepresentable
                .frame(width: .zero, height: .zero)
        }
        .sheet(isPresented: $appVariables.showingConstitutionalCriteria) {
            NavigationView {
                ConstitutionalCriteriaDefinitionsView()
                    .toolbar {
                        ToolbarItem(placement: .confirmationAction) {
                            Button("Dismiss") {
                                AppVariables.shared.showingConstitutionalCriteria = false
                            }
                        }
                    }
            }
        }
        .sheet(isPresented: $appVariables.showingResult) {
            NavigationView {
                SymptomResultView(requestBody: $requestBody, path: $path)
                    .toolbar {
                        ToolbarItem(placement: .confirmationAction) {
                            Button("Dismiss") {
                                AppVariables.shared.showingResult = false
                            }
                        }
                    }
            }
        }
    }
}

I've tried using notifications, global variables, moving the add to different views.

Desired Workflow:

  1. User fills out form
  2. User clicks Submit
  3. Interstitial ad should appear
  4. User dismisses ad
  5. Sheet should appear with result from form
0

There are 0 best solutions below