1

I am trying to add admob to my app but I get an error.

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

I have followed along on developer.google.com and on stackoverflow.com but I can't seem to fix it.

Here is my code:

import UIKit import GoogleMobileAds

class 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)
        ])
}

Thanks for anyone who can help!!

1 Answer 1

1

You have two objects of type GADBannerView (banner and bannerView) and it looks like you're mixing them.

You're trying to set adUnitId of bannerView but have never instantiated this one.

2
  • I got rid of the banner but it still shows an error
    – Nol4635
    Commented Feb 19, 2018 at 21:35
  • Can you edit your question with the new code so I can see what you've done ?
    – Omar
    Commented Feb 19, 2018 at 21:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.