Reputation: 45
so i have this test app that consists of 2 views. The parent view has a button that toggles the second view (Bottom Modal Sheet). My goal is to show an AdMob interstitial ad when the button is clicked. But still have the second view when the ad is dismissed. Currently, the ad appears but when dismissing the ad, it goes back to the parent view without the second view toggled. Is it possible to perhaps stack the Ad view on top of the second view so when dismissing the ad, the user is already in the second view?
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.")
}
}
I'm new to swiftUI so I'm not sure how to go about making this work. I understand that the problem arises because I'm trying to present two views at the same time.
The Error:
Warning: Attempt to present <_TtGC7SwiftUIP10$1c95b7a6c22SheetHostingControllerVS_7AnyView_: 0x1056344f0> on <_TtGC7SwiftUI19UIHostingControllerV14RicePurityTest11ContentView_: 0x10560f280> whose view is not in the window hierarchy!
The Interstitial 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")
}
}
Upvotes: 0
Views: 691
Reputation: 58
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")
}
}
Upvotes: 1