【问题标题】:SwiftUI - Google AdMob interstitial not showing in onAppearSwiftUI - Google AdMob 插页式广告未显示在 onAppear 中
【发布时间】:2020-05-27 03:33:15
【问题描述】:

我正在尝试在出现时显示来自 google AdMob 的插页式广告,但它没有显示并且在控制台中显示“未准备好”。我已经查看了许多其他教程和堆栈溢出页面,但还没有找到答案。谁能帮我?这是我的代码:

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")
                }



    })
}
}

【问题讨论】:

    标签: ios swift admob swiftui interstitial


    【解决方案1】:

    如 cmets 中所述,Google Admob 强烈反对使用 interstitialDidReceiveAd 回调来展示广告。相反,您可以通知您的视图广告已加载,然后您的视图应该决定是否是显示插页式广告的正确时间。所以只需创建一个带有 load 和 show 函数的辅助类,如下所示:-

    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
        }
    }
    

    现在只需在您的视图中使用此类,每次调用 load 方法时,它都会创建一个新请求(根据 doc 是必需的)。您现在应该从您的视图中观察 isLoaded 布尔值,并且可以仅在预期触发屏幕上的广告(使用 showAd)而不是在加载后立即触发,因为我们需要考虑用户可能在中间的东西......

    【讨论】:

      【解决方案2】:

      GADInterstitial.load 是异步操作,不会等到广告加载完毕,所以如果要在加载后立即显示添加,则必须使用委托。

      这是一个可能的解决方案

      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)
              })
          }
      }
      

      【讨论】:

      • 这给了我两个错误,一个在第一行显示“从类 'NSObject' 和 'GADInterstitialDelegate' 进行多重继承”,另一个在“self.interstitial = self.adDelegate”行说“无法将'GADInterstitialDelegate'类型的值分配给'GADInterstitial'类型”。你能帮我吗?
      • 谢谢,但现在我关闭广告后它又弹出来了,你知道有什么办法可以解决吗?
      • @RyanD,好吧,onAppear 回调中的初始代码加载广告(每次出现视图时都会调用),这就是原因。由于推荐的广告应该在某些用户操作上显示,所以只需通过用户工作流程放置加载代码。
      • 您是否知道是否可以调整插页式广告的大小以使其不占据整个屏幕?
      • 不要使用interstitialDidReceiveAd 事件来显示插页式广告。这可能会导致糟糕的用户体验。相反,请在需要展示之前预加载广告。然后检查GADInterstitial 上的isReady 方法以确定它是否已准备好显示。见developers.google.com/admob/ios/…
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多