Good, what I want to do is that when a button is pressed, a view is displayed with advertising to be more accurate AdMob's Interstitial. I have a problem since the button is in a view and when I click it, this% fatal error: unexpectedly found nil while unwrapping an Optional value
error occurs and the application stops working.
I leave the code.
var interstitial: GADInterstitial!
func createAndLoadAd() -> GADInterstitial {
let ad = GADInterstitial(adUnitID: "XXX-XXX-XXX")
let request = GADRequest()
request.testDevices = ["XXXXXXXXXXXXX"]
ad.loadRequest(request)
return ad
}
func MuestraPublicidad() {
if (self.interstitial.isReady)
{
self.interstitial.presentFromRootViewController(self)
self.interstitial = self.createAndLoadAd()
}
}
In The UIview I've done it in the following way
let VistaPrincipal = ViewController()
VistaPrincipal.MuestraPublicidad()