从按钮显示插页式广告,该按钮还可以从 sheet 切换另一个视图

Showing Interstitial Ad from button that also toggles another view from sheet

所以我有这个包含 2 个视图的测试应用程序。父视图有一个切换第二个视图的按钮(Bottom Modal Sheet)。我的目标是在点击按钮时显示 AdMob 插页式广告。但是当广告被关闭时仍然有第二种观点。目前,广告出现了,但是当关闭广告时,它会返回到父视图而不会切换第二个视图。是否可以将广告视图堆叠在第二个视图之上,以便在关闭广告时用户已经在第二个视图中?

import GoogleMobileAds
import SwiftUI
import UIKit

struct ContentView: View {
    @State var showingTest = false
    @State var showingDisclaimer = false
    
    //Ad
    var interstitial: Interstitial
    
    init() {
        self.interstitial = Interstitial()
    }
    
    var body: some View {
        // QuestionsView()
        // NavigationView {
        VStack {
            
            Button(action: {
                self.showingTest.toggle()
                self.interstitial.showAd()
                }) {
                Text("take the test!")
                    .fontWeight(.bold)
                    .foregroundColor(Color.white)
            }
            .frame(minWidth: 0, maxWidth: .infinity)
            .padding()
            .foregroundColor(.gray)
            .background(Color("raisinblack"))
            .cornerRadius(10)
            .font(.title)
            .sheet(isPresented: $showingTest) {
                QuestionsView()
            }.padding()
            .shadow(radius: 5)
        }
        .padding()
        .frame(minWidth: 0, maxWidth: .infinity)
        .navigationBarTitle("rice purity test.")
    }
}

我是 swiftUI 的新手,所以我不确定如何着手进行这项工作。我知道问题的出现是因为我试图同时呈现两个视图。

错误:

Warning: Attempt to present <_TtGC7SwiftUIP10c95b7a6c22SheetHostingControllerVS_7AnyView_: 0x1056344f0> on <_TtGC7SwiftUI19UIHostingControllerV14RicePurityTest11ContentView_: 0x10560f280> whose view is not in the window hierarchy!

插屏Class


final class Interstitial: NSObject, GADInterstitialDelegate {
    var interstitial: GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    
    override init() {
        super.init()
        self.LoadInterstitial()
    }
    
    func LoadInterstitial() {
        let req = GADRequest()
        self.interstitial.load(req)
        self.interstitial.delegate = self
    }
    
    func showAd() {
        if self.interstitial.isReady {
            let root = UIApplication.shared.windows.first?.rootViewController
            
            self.interstitial.present(fromRootViewController: root!)
        } else {
            print("Not Ready")
        }
    }
    
    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        self.LoadInterstitial()
        
    }
    
    /// Tells the delegate an ad request succeeded.
    func interstitialDidReceiveAd(_ ad: GADInterstitial) {
        print("interstitialDidReceiveAd")
    }
    
    /// Tells the delegate an ad request failed.
    func interstitial(_ ad: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) {
        print("interstitial:didFailToReceiveAdWithError: \(error.localizedDescription)")
    }
    
    /// Tells the delegate that an interstitial will be presented.
    func interstitialWillPresentScreen(_ ad: GADInterstitial) {
        print("interstitialWillPresentScreen")
    }
    
    /// Tells the delegate the interstitial is to be animated off the screen.
    func interstitialWillDismissScreen(_ ad: GADInterstitial) {
        print("interstitialWillDismissScreen")
        //showingTest.toggle()
    }
    
    /// Tells the delegate that a user click will open another app
    /// (such as the App Store), backgrounding the current app.
    func interstitialWillLeaveApplication(_ ad: GADInterstitial) {
        print("interstitialWillLeaveApplication")
    }
}
func showAd() {
    if self.interstitial.isReady {
        let root = UIApplication.shared.windows.last?.rootViewController
        // you can also use: UIApplication.shared.keyWindow.rootViewController
           self.interstitial.present(fromRootViewController: root!)
    } else {
        print("Not Ready")
    }
}