在 SwiftUI (5) 和 Xcode (12.4) 中实现 AdMob 插页式广告

时间:2021-06-28 18:37:24

标签: xcode swiftui admob

我正在我的应用中实施插页式广告,但对 Admob 提供的文档和新的 SwiftUI 应用结构有些困惑。

这是 app.swift 文件,显示我已经实现了 GoogleMobileAds 并在 didFinishLaunchingWithOptions 方法中启动了它。

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
    }

}

在我的 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 文档仍然展示了如何使用类 ViewControllers 来实现,我想弄清楚如何做到这一点是 SwiftUI .

1 个答案:

答案 0 :(得分:0)

为了在最新的 SwiftUI 版本中使用 Admob 文档,您需要更改此行...

        .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
                                                }
                                     // Change these two lines of code
                                     interstitial = ad
                                                
                                     interstitial?.fullScreenContentDelegate = self
                                     // To...
                                     interstitial = ad
                                     let root = UIApplication.shared.windows.first?.rootViewController                                        
                      self.interstitial!.present(fromRootViewController: root!)
                                              }
                            )
        
                })