简体   繁体   English

SwiftUI:Admob 插页式广告未在 rootViewController 上呈现

[英]SwiftUI: Admob Interstitial Ad is not being presented on rootViewController

CODED IN SWIFTUI编码 SWIFTUI

I have implemented a settings page within my SwiftUI app that is presented upon clicking the button within the view.我在我的 SwiftUI 应用程序中实现了一个设置页面,该页面在单击视图中的按钮时显示。 This view is wrapped within a sheet.这个视图被包裹在一张纸中。 You can see the code below:你可以看到下面的代码:

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

I have implemented an interstitial within this "OptionsTab" that is created when the options tab is loaded within the.onAppear() call and presented when the back button is selected.我在此“OptionsTab”中实现了一个插页式广告,该插页式广告是在选项选项卡在 .onAppear() 调用中加载时创建的,并在选择后退按钮时显示。 You can the code from the OptionsTab below:您可以从下面的 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) 
}

I have additional code within the button that logs the status of the interstitial, and it shows it as being READY, and the present code is being hit... but it never presents itself.我在记录插页式广告状态的按钮中有额外的代码,它显示它已准备好,并且当前代码正在被点击......但它永远不会出现。

I have implemented this exact code in the ContentView, and the interstitial loads perfectly fine.我已经在 ContentView 中实现了这个确切的代码,并且插页式加载非常好。 Something about this code being within the sheet is causing the interstitial not to present.工作表中有关此代码的某些内容导致插页式广告不显示。

Is the rootViewController the incorrect view to load the interstitial on when in a view that is presented from a sheet?在从工作表呈现的视图中,rootViewController 是否是加载插页式广告的不正确视图? Otherwise, what else am I doing wrong?否则,我还做错了什么?

Thanks for the help in advance!我在这里先向您的帮助表示感谢!

Try this尝试这个

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

And then add the state interstitial variable within this view, and onAppear of the ContextView, create the interstitial there.然后在此视图中添加 state 插页式变量,以及 ContextView 的 onAppear,在那里创建插页式。

Solution 2:解决方案2:

(not tested but should work): present on the presentingViewController instead of the rootViewController (未测试但应该可以):出现在presentingViewController而不是rootViewController

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

check the reason, at the end of the answer检查原因,在答案的最后

Solution 1:解决方案1:

it sounds like that there's something that prevents Interstitial ad to be presented when a sheet view is already presented听起来好像有一些东西阻止了在已经呈现工作表视图时呈现插页式广告

in my case i wanted to present the ad immediately after the sheet view is presented, which didn't work就我而言,我想在呈现工作表视图后立即呈现广告,但这不起作用

what worked is, Presenting the ad, and after it being dismissed, the sheet view will be presented , and it worked!有效的是,展示广告,在它被关闭后,将展示工作表视图,并且它有效!

for the Interstitial i used this code对于插页式广告,我使用了此代码

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

here we pass whatever we want to happen to the closure which will be called when interstitialDidDismissScreen is called在这里,我们将我们想要发生的任何事情传递给闭包,当interstitialDidDismissScreen被调用时将被调用

now instead of switching the state when button is pressed, it will be switched after the ad is dimissed现在不是在按下按钮时切换 state,而是在广告关闭后切换

at the top of your contentView:在 contentView 的顶部:

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

then in your button:然后在您的按钮中:

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

            }              

        })

Note: i used the code of interstitial Ad from this gist注意:我使用了这个gist中的插页式广告代码

Also, AFAIK we can't tell if it's an bug in swiftUI, or it's something googleAdmob was supposed to take care of?, there's no SwiftUI support From google so we can't blame them.此外,AFAIK 我们无法判断它是 swiftUI 中的错误,还是 googleAdmob 应该处理的问题?谷歌没有 SwiftUI 支持,所以我们不能责怪他们。

the problem AFAIK, that in iOS 13, grabbing the root ViewController will return the View Controller that presented the sheet, and when trying to present the ad, UIKit will complain that you are trying to present A view controller while a view controller is already present(the sheet), Solution? the problem AFAIK, that in iOS 13, grabbing the root ViewController will return the View Controller that presented the sheet, and when trying to present the ad, UIKit will complain that you are trying to present A view controller while a view controller is already present (表),解决方案? simple, present the Ad, on the presented view Controller(The sheet)简单,在呈现的视图控制器(工作表)上呈现广告

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

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