我尝试将非页内广告整合到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)
}
}
}
我推荐KAVSOFT的视频