Search code examples
iosswiftsprite-kitiad

iAd interstitial ads crashing with error "unexpectedly found nil while unwrapping an optional value"


I have code for my interstitial ads in my GameScene, and every time they're supposed to be called, I get the error in the title with this line:

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    iAdInterstitialView = SKView()
   iAdInterstitialView.frame = self.view!.bounds //This line
view?.addSubview(iAdInterstitialView)

   interstitialAd.presentInView(iAdInterstitialView)
UIViewController.prepareInterstitialAds()
}

Has anyone else had this problem and fixed it? Thank you in advance.


Solution

  • It means that unwrapped value is nil in your self.view!. You can check it this way:

    if let view = self.view {
        iAdInterstitialView.frame = view.bounds
    }