我正在尝试使用 Google 的文档在 SwiftUI 项目中实现 AdMob 开放广告:https://developers.google.com/admob/ios/app-open-ads。问题是文档完全是使用 AppDelegate 编写的。
我尝试通过在 @main 上方使用此方法添加 AppDelegate 类来实现打开广告,但它根本不起作用。没有错误,但也没有广告。
class AppDelegate: UIResponder, UIApplicationDelegate, GADFullScreenContentDelegate {
let nc = NotificationCenter.default
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
GADMobileAds.sharedInstance().start(completionHandler: nil)
return true
}
var appOpenAd: GADAppOpenAd?
var loadTime = Date()
func requestAppOpenAd() {
let request = GADRequest()
GADAppOpenAd.load(withAdUnitID: "ca-app-pub-3940256099942544/5662855259",
request: request,
orientation: UIInterfaceOrientation.portrait,
completionHandler: { (appOpenAdIn, _) in
self.appOpenAd = appOpenAdIn
self.appOpenAd?.fullScreenContentDelegate = self
self.loadTime = Date()
print("Ad …Run Code Online (Sandbox Code Playgroud)