我尝试将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)
}
}
}发布于 2020-07-01 14:53:54
当间隙不在根视图中时,解决方案对我不起作用。在四处寻找之后,我终于可以让它工作了。
我用一个EnvironmentObject来保持间隙
Xcode11.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,它专门用来显示Interstitial。代码看起来很多,但大多数都是样板代码。
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
}
}几乎所有的样板代码。请注意,class类位于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)
}
}
}发布于 2020-07-03 13:17:09
你可以采取一种非常基本的方法。只要创建一个类,每当你调用它的load方法时,它就会发出一个新的请求,一旦ad被加载,它就会通知你的视图,当你想要显示你的广告时,只需调用它的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
}
}发布于 2021-09-18 17:02:44
重要的!很简单:在创建根视图时,您需要从层次结构中选择最后一个视图,然后广告将从任何视图(例如,工作表)中显示
让root = UIApplication.shared.windows.last?.rootViewController
func showAd () {
if let interstitial = interstitialAd {
let root = UIApplication.shared.windows.last?.rootViewController
interstitial.present(fromRootViewController: root!)
}
}https://stackoverflow.com/questions/58440644
复制相似问题