gpt4 book ai didi

admob - SwiftUI 中的 Google AdMob 插页式广告

转载 作者:行者123 更新时间:2023-12-03 16:38:12 26 4
gpt4 key购买 nike

我尝试在 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 View 中输入 View
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)
}

}

}

最佳答案

当插页式广告不在 Root View 中时,该解决方案对我不起作用。在四处寻找之后,我终于可以让它工作了。
我使用 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 类来保存用于专门显示 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
}
在这里,我们实现了 GADInterstitialDelegate,当我第一次看到 Coordinator 时,它看起来很奇特。但是当你 Handlebars 弄脏时,你会发现它非常直接和方便。您需要 Coordinator 事物在委托(delegate)/UIKit 和 SwiftUI 之间进行通信。
sharing.interstitial.delegate = context.coordinator
以上是您将协调器设置为 GADInterstitialDelegate 的代码,即使您不实现委托(delegate)方法,它也可能工作得很好。但是,当您研究和调试事物的工作方式时,这些方法会很有帮助。
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 结构内。我使用委托(delegate)方法来确定何时再次弹出和预加载广告。
func showAd(_ controller:UIViewController){

if sharing.interstitial.isReady{
sharing.interstitial.present(fromRootViewController: controller)
sharing.popInterstitial = false
}else{
print("Not Ready Yet")
}
}
}
在 ContentView 或其他 View 中,找到一个在 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)
}

}
}

关于admob - SwiftUI 中的 Google AdMob 插页式广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58440644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com