我正在使用 Google AdMob 向我的 SwiftUI 应用程序添加广告,我遇到了以下情况。我在某些视图中有一个按钮,点击后会显示一个广告。不过,我也可以根据需要多次点击该按钮,每次点击都会显示一个新的不同广告。
这是插页式广告的代码
final class Interstitial:NSObject, GADInterstitialDelegate{
var interstitial: GADInterstitial = GADInterstitial(adUnitID: TestKey)
override init() {
super.init()
LoadInterstitial()
}
func LoadInterstitial(){
let req = GADRequest()
self.interstitial.load(req)
self.interstitial.delegate = self
}
func showAd(){
if self.interstitial.isReady {
let root = UIApplication.shared.windows.first?.rootViewController
self.interstitial.present(fromRootViewController: root!)
self.LoadInterstitial()
}
else{
print("Not Ready")
}
}
func interstitialDidDismissScreen(_ ad: GADInterstitial) {
self.interstitial = GADInterstitial(adUnitID: TestKey)
}
}
这是我用于测试的视图
struct Add: View {
@State var interstitial:Interstitial = Interstitial()
var body : some View{
Button(action: {
self.interstitial.showAd()
self.interstitial = Interstitial()
}){
Text("My Button")
}
}
}
由于我处于测试模式,我不确定当我转向真正的广告时,它们是否会是独一无二的并且与之前的广告有所不同。我还用一个真实的广告单元进行了尝试,它不断地一遍又一遍地展示同一个广告。
非常感谢任何帮助和指导!