Nic*_*s M 5 integration interstitial admob swiftui
我尝试在 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")
}
Run Code Online (Sandbox Code Playgroud)
但我不知道他的代码放在哪里以及如何在我的 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)
}
}
}
Run Code Online (Sandbox Code Playgroud)
当插页式广告不在根视图中时,该解决方案对我不起作用。经过一番搜索,终于可以让它工作了。
我使用环境对象来保存插页式广告
Xcode 11.5、斯威夫特 5
import Foundation
import GoogleMobileAds
class SharedValues:ObservableObject{
@Published var interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
@Published var popInterstitial = false
}
Run Code Online (Sandbox Code Playgroud)
然后创建一个struct或viewcontroller类来保存专门用于显示Interstitial的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
}
Run Code Online (Sandbox Code Playgroud)
这里我们实现了 GADInterstitialDelegate,第一次看到 Coordinator 的时候我觉得它很奇怪。但当你亲自动手时,你会发现它非常简单和方便。您需要 Coordinator 来在 delegate/UIKit 和 SwiftUI 之间进行通信。
sharing.interstitial.delegate = context.coordinator
Run Code Online (Sandbox Code Playgroud)
上面是将协调器设置为 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
}
}
Run Code Online (Sandbox Code Playgroud)
几乎所有样板代码。请注意,Cooperative 类位于 InterstitialVC 结构内部。我使用委托方法来确定何时再次弹出并预加载广告。
func showAd(_ controller:UIViewController){
if sharing.interstitial.isReady{
sharing.interstitial.present(fromRootViewController: controller)
sharing.popInterstitial = false
}else{
print("Not Ready Yet")
}
}
Run Code Online (Sandbox Code Playgroud)
}
在 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)
}
}
}
Run Code Online (Sandbox Code Playgroud)