【问题标题】:SwiftUI: Admob Interstitial Ad is not being presented on rootViewControllerSwiftUI:Admob 插页式广告未在 rootViewController 上呈现
【发布时间】:2020-04-10 02:44:52
【问题描述】:

在 SWIFTUI 中编码

我在我的 SwiftUI 应用程序中实现了一个设置页面,该页面在单击视图中的按钮时显示。这个视图被包裹在一张纸中。可以看下面的代码:

TabsBar(showOptionsTab: $showOptionsTab)
    .sheet(isPresented: $showOptionsTab)
    {
        OptionsTab(showOptionsTab: self.$showOptionsTab)
    }

我在此“OptionsTab”中实现了一个插页式广告,该插页式广告是在 .onAppear() 调用中加载选项选项卡时创建的,并在选择后退按钮时显示。您可以从下面的 OptionsTab 中获取代码:

@State private var interstitial : GADInterstitial!

VStack
{
    ... other code

    Button(action:
    {
        if ( self.interstitial.isReady)
        { 
            let root = UIApplication.shared.windows.first?.rootViewController
            self.interstitial.present(fromRootViewController: root!)               
        }

        self.showOptionsTab.toggle()
    })
    {
        Image(systemName: "xmark")
            .font(.largeTitle)
    }
}
.onAppear
{
    self.interstitial = GADInterstitial(adUnitID: "addID")
    let req = GADRequest()
    self.interstitial.load(req) 
}

我在按钮中有额外的代码记录了插页式广告的状态,它显示为就绪,当前的代码正在被点击……但它永远不会出现。

我已经在 ContentView 中实现了这个确切的代码,并且插页式加载非常好。此代码在工作表中的某些原因导致插页式广告不显示。

在从工作表呈现的视图中,rootViewController 是否是加载插页式广告的不正确视图?否则,我还有什么错?

提前感谢您的帮助!

【问题讨论】:

    标签: swift swiftui interstitial googlemobileads gadinterstitial


    【解决方案1】:

    试试这个

    TabsBar(showOptionsTab: $showOptionsTab)
        .sheet(isPresented: $showOptionsTab)
        {
            OptionsTab(showOptionsTab: self.$showOptionsTab)
                .OnDisapear 
                {    
                    if ( self.interstitial.isReady)
                    { 
                        let root = UIApplication.shared.windows.first?.rootViewController
                        self.interstitial.present(fromRootViewController: root!)               
                    }
                } 
            }
        }
    

    然后在这个view里面添加state interstitial变量,和ContextView的onAppear,在那里创建interstitial。

    【讨论】:

      【解决方案2】:

      解决方案 2:

      (未经测试,但应该可以): 出现在 presentingViewController 而不是 rootViewController

        if ( self.interstitial.isReady)
              { 
                  let root = UIApplication.shared.windows.first?.rootViewController?.presentingViewController
                  self.interstitial.present(fromRootViewController: root!)               
              }
      

      检查原因,在答案的末尾

      解决方案 1:

      听起来好像有什么东西阻止了在工作表视图已经呈现时显示插页式广告

      在我的情况下,我想在显示工作表视图后立即显示广告,但这不起作用

      有效的是,展示广告,在它被关闭后,将展示工作表视图,它有效!

      对于插页式广告,我使用了此代码

      import SwiftUI
      import GoogleMobileAds
      import UIKit
      
      final class Interstitial:NSObject, GADInterstitialDelegate{
        var interstitialID = "ca-app-pub-3940256099942544/4411468910"
        var interstitial:GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        var sheet: (() -> Void)? = nil  //this closure will be executed after dismissing the ad
      
        override init() {
          super.init()
          LoadInterstitial()
        }
      
        func LoadInterstitial(){
          let req = GADRequest()
          self.interstitial.load(req)
          self.interstitial.delegate = self
        }
      
        func showAd(then sheet:(() -> Void)?){
          if self.interstitial.isReady{
            let root = UIApplication.shared.windows.first?.rootViewController
            self.interstitial.present(fromRootViewController: root!)
            self.sheet = sheet
          }
          else{
            print("Not Ready")
          }
        }
      
        func interstitialDidDismissScreen(_ ad: GADInterstitial) {
          self.interstitial = GADInterstitial(adUnitID: interstitialID)
          LoadInterstitial()
          if let presentSheet = sheet {
            presentSheet()
          }
        }
      }
      

      在这里,我们将我们想要发生的任何事情传递给闭包,当interstitialDidDismissScreen 被调用时将被调用

      现在不是在按下按钮时切换状态,而是在广告关闭后切换

      在 contentView 的顶部:

          struct HistoryView: View {
            private var fullScreenAd: Interstitial!
          init() {
      fullScreenAd = Interstitial()
      }
          }
      

      然后在你的按钮中:

         Button(action: {
                  self.interstitial.showAd {
                    self.showOptionsTab.toggle()
      
                  }              
      
              })
      

      注意:我使用了来自gist的插页式广告代码

      另外,AFAIK 我们无法判断这是 swiftUI 中的错误,还是 googleAdmob 应该处理的问题?谷歌不支持 SwiftUI,所以我们不能责怪他们。

      问题 AFAIK,在 iOS 13 中,抓取根 ViewController 将返回呈现工作表的视图控制器,当尝试呈现广告时,UIKit 会抱怨你试图呈现视图控制器而视图控制器已经存在(工作表), 解决方案 ?简单,在呈现的视图控制器(工作表)上呈现广告

      【讨论】:

        猜你喜欢
        • 2021-08-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多