所以我有这个由两个视图组成的测试应用程序。父视图有一个按钮,用于切换第二个视图(底部模态图纸(。我的目标是在点击按钮时显示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 <_TtGC7SwiftUIP10$1c95b7a6c22SheetHostingControllerVS_7AnyView_: 0x1056344f0> on <_TtGC7SwiftUI19UIHostingControllerV14RicePurityTest11ContentView_: 0x10560f280> whose view is not in the window hierarchy!
间质类
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")
}
}