Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

swift - SwiftUI, Admob Not showing and freezes app

So I'm trying to integrate admob to my SwiftUI project. I have a couple of questions;

This is my Interstitial ad class;

final class Interstitial:NSObject, GADInterstitialDelegate{
    var interstitial:GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")

override init() {
    super.init()
    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")
    LoadInterstitial()
}

}

And this is how I use it;

struct page3Selected: View {

@Binding var currentPage: String

var interstitial:Interstitial
    
    init(currentPage: Binding<String>){
        self.interstitial = Interstitial()
        self._currentPage = currentPage
    }

func checkIntsAd() {
    self.interstitial.showAd()
}

var body: some View {
    GeometryReader{
        geometry in
        
        VStack{
            HStack{
                Button(action: {
                    checkIntsAd()
                    withAnimation {
                        self.currentPage = "page1"
                    }
                }) {
                    HStack{
                        Image("page1")
                        Text("page1").foregroundColor(.white).font(.footnote)
                    }.frame(width: geometry.size.width / 1.2 / 3,height: geometry.size.height / 2.35).background(Color(red: 52 / 255, green: 52 / 255, blue: 61 / 255)).cornerRadius(10).padding(.trailing, 2)
                }
                Button(action: {
                    checkIntsAd()
                    withAnimation {
                        self.currentPage = "page3"
                    }
                }) {
                    HStack{
                        Image("page3")
                        Text("page3").foregroundColor(.white).font(.footnote)
                    }.frame(width: geometry.size.width / 1.2 / 3,height: geometry.size.height / 2.35).background(Image("switch").resizable().edgesIgnoringSafeArea(.all)).padding(.leading, 2)
                }

            }
        }.padding(.leading, geometry.size.width / 20)
        
    }
}

Sometimes ad returns "Not Ready". I don't know why... When I want to use the same class for multiple structs, should I init the class again? Or should I create another class and init/use it? When I use multiple classes in multiple structs, If one ad is not ready, the other ones also returns not ready. Even sometimes my app freezes for that or it runs so slow. What is the proper way to use If I'm doing it wrong?

Thank you for your answers...

question from:https://stackoverflow.com/questions/65830517/swiftui-admob-not-showing-and-freezes-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...