import SwiftUI
import Firebase
import GoogleMobileAds
import AppTrackingTransparency
import AdSupport
import RevenueCat
class AdsManager: NSObject, ObservableObject {
final class interstitial:NSObject, GADFullScreenContentDelegate,ObservableObject{
private var interstitial: GADInterstitialAd?
override init() {
super.init()
requestInterstitialAds()
}
func requestInterstitialAds() {
let request = GADRequest()
request.scene = UIApplication.shared.connectedScenes.first as! UIWindowScene?
ATTrackingManager.requestTrackingAuthorization (completionHandler: { status in
GADInterstitialAd.load(withAdUnitID: Constants.adUnitInterstitialInstalled, request: request, completionHandler: { [self] ad, error in
if let error = error {
print("Failed to load ad with error: \(error)")
return
}
self.interstitial = ad
self.interstitial?.fullScreenContentDelegate = self
})
})
}
func showAd() {
let root = UIApplication.shared.windows.first?.rootViewController
if let fullScreenAds = interstitial {
fullScreenAds.present(fromRootViewController: root!)
} else {
print("not ready")
}
}
}
}
class AdsViewModel: ObservableObject {
static let shared = AdsViewModel()
@Published var interstitial = AdsManager.interstitial()
@Published var showInterstitial = false {
didSet {
if showInterstitial {
interstitial.showAd()
showInterstitial = false
}
else {
self.interstitial.requestInterstitialAds()
}
}
}
}
@main
struct FetalGrowthPercentile: App {
let adsVM = AdsViewModel.shared
init(){
setupRevenueCat()
GADMobileAds.sharedInstance().start(completionHandler: nil)
FirebaseApp.configure()
}
var body: some Scene {
WindowGroup {
SwitchBoard()
.environmentObject(adsVM)
.environmentObject(fetusInfoModel())
.onAppear(perform: UIApplication.shared.addTapGestureRecognizer)
}
}
func setupRevenueCat() {
Purchases.logLevel = .debug
Purchases.configure(
with: Configuration.Builder(withAPIKey: Constants.publicKey)
.with(appUserID: Constants.revenueCatAppUserId)
.build()
)
}
}
Previously, the code was working with no problem for one year. With the new xcode update, the problem began. I had blank out two lines and the problem disappeared –
setupRevenueCat()
FirebaseApp.configure()
I read RevenueCat Documentation. I tried using AppDelegate and have a function using didfinishrunningwithoptions. I would run into problem with unstable UI. I try updating cocoa pods but problem was still present.
Louis