【问题标题】:Google AdMob interstitial in SwiftUISwiftUI 中的 Google AdMob 插页式广告
【发布时间】:2019-10-17 20:57:21
【问题描述】:

我尝试在 swiftUI 中集成插页式广告,我创建了 UIViewControllerRepresentable 类和 UIViewController。

https://developers.google.com/admob/ios/interstitial#show_the_ad

但我不知道在广告准备好并加载后如何展示广告。

在 Admob 文档中我有

  if interstitial.isReady {
                    interstitial.present(fromRootViewController: viewController)
                } else {
                    print("Ad wasn't ready")
                }

但我不知道他的代码放在哪里以及如何在我的 swiftUI 视图中输入视图

import SwiftUI
import UIKit
import GoogleMobileAds

final class GADInterstitialViewController: UIViewControllerRepresentable {
    public var interstitial: GADInterstitial!
    public func makeUIViewController(context: UIViewControllerRepresentableContext<GADInterstitialViewController>) -> UIViewController {
        let interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let viewController = UIViewController()
        let request = GADRequest()
        interstitial.load(request)

        return viewController
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext<GADInterstitialViewController>) {

    }



}


struct Transit: View {


    var body: some View {
        ZStack{
            GADInterstitialViewController()
                .frame(width: screen.width, height: screen.height, alignment: .center)
        }

    }

}

【问题讨论】:

标签: admob integration swiftui interstitial


【解决方案1】:

当插页式广告不在根视图中时,该解决方案对我不起作用。找了一圈,终于搞定了。

我使用 EnvironmentObject 来保存插页式广告

Xcode 11.5、Swift 5

import Foundation
import GoogleMobileAds
class SharedValues:ObservableObject{
    
    @Published var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    @Published var popInterstitial = false

}

然后创建一个结构体或视图控制器类来保存用于专门显示插页式广告的 ViewController/View。代码看起来很多,但大多数都是样板代码。

struct InterstitialVC:UIViewControllerRepresentable{

@EnvironmentObject var sharing:SharedValues

func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
    
    if sharing.popInterstitial{
        
        showAd(uiViewController)
    }
}




func makeUIViewController(context: Context) -> some UIViewController {
    let vc = UIViewController()
    vc.view.frame = UIScreen.main.bounds

    sharing.interstitial.delegate = context.coordinator
    return vc
}

我们在这里实现了 GADInterstitialDelegate,当我第一次看到 Coordinator 时,它看起来很奇特。但是当你把手弄脏时,你会发现它非常直接和方便。您需要 Coordinator 来在委托/UIKit 和 SwiftUI 之间进行通信。

sharing.interstitial.delegate = context.coordinator

以上是您将协调器设置为 GADInterstitialDelegate 的代码,即使您不实现委托方法,它也可能工作得很好。但是,当您研究和调试事物的工作原理时,这些方法会很有帮助。

func makeCoordinator() -> Coordinator {
    Coordinator(self)
}

class Coordinator:NSObject,GADInterstitialDelegate{
    var parent:InterstitialVC
    
    init(_ parent: InterstitialVC) {
        self.parent = parent
    }
    func interstitialDidReceiveAd(_ ad: GADInterstitial) {
        //do your things
        parent.sharing.popInterstitial = true
    }
    
    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        //do your things
        let request = GADRequest()
        parent.sharing.interstitial.load(request)
    }
    
    func interstitialWillPresentScreen(_ ad: GADInterstitial) {
        //do your things
    }
    func interstitialDidFail(toPresentScreen ad: GADInterstitial) {
        //do your things
    }
}

几乎所有样板代码。请注意,坐标类位于 InterstitialVC 结构内。我使用委托方法来确定何时再次弹出和预加载广告。

func showAd(_ controller:UIViewController){
    
    if sharing.interstitial.isReady{
        sharing.interstitial.present(fromRootViewController: controller)
        sharing.popInterstitial = false
    }else{
        print("Not Ready Yet")
    }
}

}

在 ContentView 或其他视图中,在 InterstitialVC() 弹出之前找到一个放置/隐藏它的好地方。瞧

struct ContentView: View {
    @State var showInterstitial = true
    @EnvironmentObject var sharing:SharedValues
    var body: some View {
        ZStack{
//            if showInterstitial{
//                InterstitialVC()
//                   .edgesIgnoringSafeArea(.all)
//            }
            NavigationView{
                List{
                    NavigationLink("Show Second View", destination: SecondView())
                }
            }
           
            
            
        }.onAppear{
            
            let request = GADRequest()
            self.sharing.interstitial.load(request)
        }
        

    }
}

import SwiftUI
import GoogleMobileAds

struct SecondView: View {
    @EnvironmentObject var sharing:SharedValues
    
    @State var showInterstitial = true
    var body: some View {
        ZStack{
            if showInterstitial{
                
                InterstitialVC()
            }
            
            Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/)
        }
        .background(Color.yellow)
        .onAppear{
           let request = GADRequest()
           parent.sharing.interstitial.load(request)
          }
        
    }
}

【讨论】:

  • 真的很有用。谢谢。
  • 重要!很简单:创建根视图时,需要从层次结构中选择LAST视图,然后广告将从任何视图(例如,工作表)中显示让root = UIApplication.shared.windows.last?.rootViewController跨度>
【解决方案2】:

您可以采用非常基本的方法。只需创建一个类,它每次调用它的 load 方法时都会发出一个新请求,一旦加载广告,它就会通知你的视图,并且每当你想显示你的广告时,只需调用它的 show 方法,如下所示:-

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

【讨论】:

  • 我认为我应该使用 UIViewControllerRepresentable,因为上述方法(我对没有 UIViewControllerRepresentable 的常规类使用相同的方法)不会调用委托方法。
【解决方案3】:

重要!简单地: 创建根视图时,需要从层次结构中选择最后一个视图,然后广告将从任何视图(例如工作表)中显示

让 root = UIApplication.shared.windows.last?.rootViewController

func showAd () {
     if let interstitial = interstitialAd {
        let root = UIApplication.shared.windows.last?.rootViewController
        interstitial.present(fromRootViewController: root!)
     }
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    • 2020-09-21
    • 2018-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多