【问题标题】:AdMob giving a fatal error (finding nil when unwrapping optional)AdMob 给出致命错误(在展开可选时发现 nil)
【发布时间】:2016-07-31 17:26:02
【问题描述】:

我正在使用 Xcode 7.3、SpriteKit、Swift 和 AdMob。 我在显示 插页式 广告时遇到了很多麻烦 - 我也是刚刚被介绍给 GameViewController(我忘了这个文件甚至是项目的一部分,所以我可能没有正确使用它)。

这是我的 GameViewController.swift 的重要部分:

import UIKit
import SpriteKit
import GoogleMobileAds

class GameViewController: UIViewController {

    var interstitial: GADInterstitial!

    override func viewDidLoad() {
        super.viewDidLoad()
        self.interstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
        let request = GADRequest()
        // Requests test ads on test devices.
        request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
        self.interstitial.loadRequest(request)

        //I deleted the code that starts up my first scene here to save space on this post
    }
    func displayAd() {
        if self.interstitial.isReady {
            self.interstitial.presentFromRootViewController(self)
        }
    }
}

如您所见,我删除了默认出现的覆盖函数,因为这些可能不是问题的一部分(它们仍然在我的项目中,不用担心)。

然后在我的应用程序中,在不同的 swift 文件中,我称之为:

GameViewController().displayAd()

我没有收到任何错误,但是当我运行应用程序并调用该行时,我收到了常见的 fatal error: unexpectedly found nil while unwrapping an Optional value 错误。如果我在 GameViewController 文件中调用 displayAd() 函数,则不会发生这种情况(但这并没有真正的帮助,因为那不是场景)。

关于我做错了什么有什么建议吗?如果您有正确设置此广告的提示,请告诉我我已经为此工作了好几个小时@_@。

谢谢!

【问题讨论】:

    标签: ios swift admob


    【解决方案1】:

    当你调用这个函数GameViewController().displayAd()。它正在初始化 GameViewController。此时var interstitial:GADInterstitial!是零。因为这是在 viewDidLoad 之后设置的。这就是您的应用崩溃的原因。

    您需要在初始化视图控制器时初始化标识符,如下所示

    import UIKit
    import SpriteKit
    import GoogleMobileAds
    
    class GameViewController: UIViewController {
    
        var interstitial: GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
    
        override func viewDidLoad() {
            super.viewDidLoad()
            let request = GADRequest()
            // Requests test ads on test devices.
            request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
            self.interstitial.loadRequest(request)
    
            //I deleted the code that starts up my first scene here to save space on this post
        }
        func displayAd() {
            if self.interstitial.isReady {
                self.interstitial.presentFromRootViewController(self)
            }
        }
    }
    

    但我认为如果 GameViewController 没有加载到屏幕上,那么我怀疑它是否会展示您的广告。您应该调用 AppDelegate displayAdFrom Window RootViewController。

    更新

    class AppDelegate: UIResponder, UIApplicationDelegate {
    
        var window: UIWindow?
    
        func displayAds() {
           let interstitial: GADInterstitial = GADInterstitial(adUnitID: "ca-app-pub-3940256099942544/4411468910")
           let request = GADRequest()
           // Requests test ads on test devices.
           request.testDevices = ["2077ef9a63d2b398840261c8221a0c9b"]
           self.interstitial.loadRequest(request)
           if self.interstitial.isReady {
               self.interstitial.presentFromRootViewController(window.rootViewController)
           }
        }
    
    }
    

    你可以调用这个函数 (UIApplication.sharedApplication().delegate as!AppDelegate).displayAds。

    没有经验。如果仍有问题,您将不得不自己挖掘。

    【讨论】:

    • 好吧,你解决了我的问题(所以谢谢!),但就像你说的那样,现在它被调用了。我应该在哪里称呼您建议的 AppDelegate 东西?我不是以正常的方式做这件事,因为我试图完全按照谷歌在他们的教程中建议的去做。
    • 如果此代码没有显示您的广告 GameViewController().displayAd() 则尝试使用 self.interstitial.presentFromRootViewController(*AppDelegate Window RootViewController*)
    • 我尝试添加self.interstitial.presentFromRootViewController(*AppDelegate Window RootViewController*),但它不允许程序运行。它建议我在 AppDelegate 和 Window 之后放置逗号,但这并不能解决问题。我也注意到我的方法不起作用的原因是,无论我等待多长时间,广告都没有“准备好”。也许我设置了其他错误并且它没有尝试准备和广告?
    • 添加了一些更新。关于如何从 AppDelegate 进行演示。
    猜你喜欢
    • 1970-01-01
    • 2015-10-12
    • 2023-03-09
    • 2016-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多