SwiftUI中的Google AdMob插页式广告

时间:2019-10-17 20:57:21

标签: admob integration swiftui interstitial

我尝试将非页内广告整合到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)
        }

    }

}

3 个答案:

答案 0 :(得分:2)

插页式广告不在根视图中时,该解决方案对我不起作用。经过搜索,终于可以使它工作了。

我使用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

}

,然后创建一个struct或viewcontroller类,以保存用于专门显示插页式广告的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,对协调员来说,第一次看到它对我来说似乎很奇怪。但是当您弄脏手时,您会发现它非常简单,方便。您需要协调器来在委托/ 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
    }
}

几乎所有样板代码。请注意,Coordinate类位于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)
          }
        
    }
}

答案 1 :(得分:1)

答案 2 :(得分:0)

您可以采用非常基本的方法。只需创建一个类,即可在每次调用其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
    }
}