简体   繁体   English

SwiftUI - Google AdMob 插页式广告未在 onAppear 中显示

[英]SwiftUI - Google AdMob interstitial not showing in onAppear

I am trying to show an interstitial ad from google AdMob on appearing, but It Does not show up and it says "not ready" in the console.我试图在出现时显示来自谷歌 AdMob 的插页式广告,但它没有显示,并且在控制台中显示“未准备好”。 I have checked out many other tutorials and stack overflow pages on it but haven't found the answer.我已经查看了许多其他教程和堆栈溢出页面,但没有找到答案。 Can anyone help me?谁能帮我? Here is my code:这是我的代码:

struct ContentView: View {
@State var interstitial: GADInterstitial!
var body: some View{
    Text("Some Text").onAppear(perform: {
        self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let req = GADRequest()
        self.interstitial.load(req)


                if self.interstitial.isReady{
                        let root = UIApplication.shared.windows.first?.rootViewController
                        self.interstitial.present(fromRootViewController: root!)
                    }else {
                    print("not ready")
                }



    })
}
}

As mentioned in the comments using interstitialDidReceiveAd callback for showing ad is highly discouraged by Google Admob. Instead you can notify your view that Ad is loaded and then your view should take the decision that is it the correct time to show Interstitial Ad or not.正如谷歌 Admob 强烈反对使用interstitialDidReceiveAd回调显示广告的评论中所述。相反,您可以通知您的视图广告已加载,然后您的视图应决定是否是显示插页式广告的正确时间。 So just create a helper class with a load and show function as shown:-因此,只需创建一个带负载的助手 class 并显示function,如下所示:-

import Foundation
import GoogleMobileAds
import UIKit
    
final class InterstitialAd : NSObject, GADInterstitialDelegate, ObservableObject {
    var interstitial: GADInterstitial? = nil
    @Published var isLoaded: Bool = false
    
    func LoadInterstitial() {
        interstitial = GADInterstitial(adUnitID: Constants.interstitialAdCode)
        let req = GADRequest()
        interstitial!.load(req)
        interstitial!.delegate = self
    }
    
    func showAd() {
        if let fullScreenAd = self.interstitial, fullScreenAd.isReady {
           let root = UIApplication.shared.windows.first?.rootViewController
           fullScreenAd.present(fromRootViewController: root!)
           isLoaded = false
        }
    }
    
    func interstitialDidReceiveAd(_ ad: GADInterstitial) {
        isLoaded = true
    }
}

Now just use this class in your view and every time you call the load method it creates a new request (required as per doc).现在只需在您的视图中使用此 class ,每次调用加载方法时,它都会创建一个新请求(根据文档需要)。 You should now observe the isLoaded Boolean from your View and can trigger the Ad on screen (using showAd) only when its intended to and not as soon as its loaded because we need to consider that user could be in middle of something...您现在应该从您的视图中观察isLoaded Boolean 并且可以仅在其预期时触发屏幕上的广告(使用 showAd),而不是在加载后立即触发,因为我们需要考虑用户可能正在处理某事......

The GADInterstitial.load is asynchronous operation and don't wait till ad is loaded, so you have to use delegate if you want to show add immediately after load. GADInterstitial.load是异步操作,不会等到广告加载完毕,所以如果要在加载后立即显示添加,则必须使用委托。

Here is a possible solution这是一个可能的解决方案

class MyDInterstitialDelegate: NSObject, GADInterstitialDelegate {

    func interstitialDidReceiveAd(_ ad: GADInterstitial) {
        if ad.isReady{
            let root = UIApplication.shared.windows.first?.rootViewController
            ad.present(fromRootViewController: root!)
        } else {
            print("not ready")
        }
    }
}

struct ContentView: View {
    @State var interstitial: GADInterstitial!
    private var adDelegate = MyDInterstitialDelegate()
    var body: some View{
        Text("Some Text").onAppear(perform: {
            self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
            self.interstitial.delegate = self.adDelegate

            let req = GADRequest()
            self.interstitial.load(req)
        })
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM