我正在努力在我的应用程序中实现插页式广告,但对 AdMob 提供的文档和新的 SwiftUI 应用程序结构遇到了一些困惑。
这里是app.swift
文件,显示我已经实现了 GoogleMobileAds 并在didFinishLaunchingWithOptions
method.
import SwiftUI
import GoogleMobileAds
@main
struct AdamsCalcApp: App {
var calculator = Calculator()
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView().environmentObject(calculator)
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
// Setup Google AdMob instance
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
}
In my ContentView.swift
文件中,我创建了间隙变量,如下所示:
@State var interstitial: GADInterstitialAd?
然后,在视图的主堆栈上,我调用onAppear(perform:)
加载广告:
.onAppear(perform: {
let request = GADRequest()
GADInterstitialAd.load(withAdUnitID:"ca-app-pub-3940256099942544/4411468910",
request: request,
completionHandler: { [self] ad, error in
if let error = error { return }
interstitial = ad
interstitial?.fullScreenContentDelegate = self
}
)
})
但是,我不断收到此错误:
“无法将‘ContentView’类型的值分配给‘GADFullScreenContentDelegate’类型?”
在尝试了几种不同的解决方法并尝试查找与我类似的设置后,我感觉有点无能为力。 AdMob 文档仍然只展示如何实现它UIViewController
但我想知道如何在 SwiftUI 中做到这一点。