swift - 集成 adMob : Error: Thread 1: Fatal error:

标签 swift admob swift4

我正在尝试将 admob 添加到我的应用程序,但出现错误。

线程 1: fatal error :在展开可选值时意外发现 nil

我已经在 developer.google.com 和 stackoverflow.com 上进行了跟踪,但我似乎无法修复它。

这是我的代码:

导入 UIKit 导入 GoogleMobileAds

ViewController 类:UIViewController、GADBannerViewDelegate {

//Place your instance variables here
let allQuestions = QuestionBank()
var pickedAnswer : Bool = false
var questionNumber : Int = 0
var score : Int = 0
var bannerView: GADBannerView!
var banner: GADBannerView!


@IBOutlet weak var questionLabel: UILabel!
@IBOutlet weak var scoreLabel: UILabel!
@IBOutlet weak var GoogleBannerView: GADBannerView!

@IBOutlet weak var progressLabel: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    banner = GADBannerView(frame: self.view.frame)
    GADMobileAds.configure(withApplicationID: "ca-app-pub-3940256099942544/2934735716")
    bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"       ***//Crashes Here***
    bannerView.rootViewController = self
    bannerView.load(GADRequest())
    bannerView.delegate = self

    /// Tells the delegate an ad request loaded an ad.
    func adViewDidReceiveAd(_ bannerView: GADBannerView) {
        print("adViewDidReceiveAd")
    }

    /// Tells the delegate an ad request failed.
    func adView(_ bannerView: GADBannerView,
                didFailToReceiveAdWithError error: GADRequestError) {
        print("adView:didFailToReceiveAdWithError: \(error.localizedDescription)")
    }

    /// Tells the delegate that a full-screen view will be presented in response
    /// to the user clicking on an ad.
    func adViewWillPresentScreen(_ bannerView: GADBannerView) {
        print("adViewWillPresentScreen")
    }

    /// Tells the delegate that the full-screen view will be dismissed.
    func adViewWillDismissScreen(_ bannerView: GADBannerView) {
        print("adViewWillDismissScreen")
    }

    /// Tells the delegate that the full-screen view has been dismissed.
    func adViewDidDismissScreen(_ bannerView: GADBannerView) {
        print("adViewDidDismissScreen")
    }

    /// Tells the delegate that a user click will open another app (such as
    /// the App Store), backgrounding the current app.
    func adViewWillLeaveApplication(_ bannerView: GADBannerView) {
        print("adViewWillLeaveApplication")
    }

    bannerView = GADBannerView(adSize: kGADAdSizeBanner)

    addBannerViewToView(bannerView)

    nextQuestion()
}

func addBannerViewToView(_ bannerView: GADBannerView) {
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(bannerView)
    view.addConstraints(
        [NSLayoutConstraint(item: bannerView,
                            attribute: .bottom,
                            relatedBy: .equal,
                            toItem: bottomLayoutGuide,
                            attribute: .top,
                            multiplier: 1,
                            constant: 0),
         NSLayoutConstraint(item: bannerView,
                            attribute: .centerX,
                            relatedBy: .equal,
                            toItem: view,
                            attribute: .centerX,
                            multiplier: 1,
                            constant: 0)
        ])
}

感谢任何可以提供帮助的人!!

最佳答案

您有两个 GADBannerView 类型的对象(banner 和 bannerView),看起来您正在混合它们。

您正在尝试设置 bannerView 的 adUnitId,但从未实例化过它。

关于swift - 集成 adMob : Error: Thread 1: Fatal error:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48873858/

相关文章:

ios - Facebook 在 ios 问题中使用 firebase 登录

arrays - 如何在时间复杂度 < O(n^2) 和空间复杂度 O(n) 中使用 swift 首先按值排序整数数组,然后按重复次数排序整数数组

swift4 - 将页面控件设置到 UIPageViewController 的顶部

gradle - 在某些设备上获取 admob 插页式广告的 java.lang.NoClassDefFoundError

android - "Timed out waiting for ad response",尝试将 admob 添加到我的应用程序时

android - glsurfaceview 忽略 admob 高度

swift - xcode 9.0.1/swift 4,没有使用 Objective-C 选择器 'onClick:forEvent:' 声明的方法

ios - SKShapeNode 拖动不流畅

swift - DynamoDB Swift 表扫描映射类

ios - 字符串扩展 - 收到错误 'No Member'