Swift 在AppDelegate中预加载adMob间隙广告,并在其他UIViewController中显示广告

Swift 在AppDelegate中预加载adMob间隙广告,并在其他UIViewController中显示广告,swift,admob,appdelegate,preload,interstitial,Swift,Admob,Appdelegate,Preload,Interstitial,我正在尝试在AppDelegate:didFinishLaunchingWithOptions中预加载间隙广告,然后在另一个UIViewController的ViewWillAspect方法中显示它。 我已经搜索了几个小时,答案要么是objective-C,我无法完全理解/转换为Swift,要么就是不起作用。请帮我指出正确的方向 class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {

我正在尝试在AppDelegate:didFinishLaunchingWithOptions中预加载间隙广告,然后在另一个UIViewController的ViewWillAspect方法中显示它。 我已经搜索了几个小时,答案要么是objective-C,我无法完全理解/转换为Swift,要么就是不起作用。请帮我指出正确的方向

class AppDelegate: UIResponder, UIApplicationDelegate, GADInterstitialDelegate {

    var window: UIWindow?
    var interstitial: GADInterstitial!
    var gViewController = UIViewController()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        FIRApp.configure()

        GADMobileAds.configure(withApplicationID: "MY APP ID")

        createAndLoadInterstitial()

        return true
    }


    func createAndLoadInterstitial() -> GADInterstitial {
        interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        interstitial.delegate = self
        interstitial.load(GADRequest())
        return interstitial
    }

    func interstitialDidReceiveAd(_ ad: GADInterstitial) {

        interstitial.present(fromRootViewController: self.gViewController)

    }

    func interstitialDidDismissScreen(_ ad: GADInterstitial) {
        interstitial = createAndLoadInterstitial()
    }

}
在其他UIViewController中:

var interstitial: GADInterstitial!

 override func viewWillAppear(_ animated: Bool) {

    let App = UIApplication.shared.delegate as! AppDelegate

    App.gViewController = self

    if interstitial.isReady {

        interstitial.present(fromRootViewController: self)

    } else {

        print("Ad not ready")

    }


}

给出错误:在展开可选值时意外发现nil

您有两个独立的
interstitual
对象,一个在类
AppDelegate
中,另一个在类
UIViewController
中,只有类
AppDelegate
中的一个被启动,这就是为什么在类
UIViewController
中访问
interstitual
时,它仍然是
nil

如果要从
UIViewController
访问
AppDelegate
中的变量:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }
    // Now you can access it
    if appDelegate.interstitial.isReady {
        appDelegate.interstitial.present(fromRootViewController: self)
    } else {
        print("Ad not ready")
    }
}

您有两个独立的
interstitual
对象,一个在类
AppDelegate
中,另一个在类
UIViewController
中,只有一个在类
AppDelegate
中被启动,这就是为什么在访问类
UIViewController
中的
interstitutial
时仍然是
nil
的原因

如果要从
UIViewController
访问
AppDelegate
中的变量:

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }
    // Now you can access it
    if appDelegate.interstitial.isReady {
        appDelegate.interstitial.present(fromRootViewController: self)
    } else {
        print("Ad not ready")
    }
}

为什么不将其加载到UIViewController中?因为所述UIViewController只是一个菜单,所以用户可能会很快(在广告准备好之前)单击某个按钮。为什么不将其加载到UIViewController中?因为所述UIViewController只是一个菜单,所以用户可能会很快单击某个按钮(在广告准备好之前)。