My iAd works fine when there is internet connection, but when there is no internet, it presents a grey screen. My code can also handle when I set the simulator to give errors by switching to the next screen. I have tried to check to see if the ad is loaded, and transition if it isn't, but that automatically transitions to the next screen.
My code:
class TransistionScene: SKScene, ADInterstitialAdDelegate {
var interAd = ADInterstitialAd()
var interAdView = UIView()
var closeButton = UIButton()
var adLoaded = false
override func didMoveToView(view: SKView) {
closeButton.frame = CGRectMake(20, 20, 30, 30)
closeButton.layer.cornerRadius = 15
closeButton.setTitle("x", forState: .Normal)
closeButton.setTitleColor(UIColor.blackColor(), forState: .Normal)
closeButton.backgroundColor = UIColor.whiteColor()
closeButton.layer.borderColor = UIColor.blackColor().CGColor
closeButton.layer.borderWidth = 1
loadAd()
closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown)
}
func close(sender: UIButton) {
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
adLoaded = false
let myScene = GameOver(size: self.size)
myScene.scaleMode = self.scaleMode
let transition = SKTransition.flipHorizontalWithDuration(1.0)
self.view?.presentScene(myScene, transition: transition)
}
func loadAd() {
interAd = ADInterstitialAd()
interAd.delegate = self
}
func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
interAdView = UIView()
interAdView.frame = self.view!.frame
view!.addSubview(interAdView)
interAd.presentInView(interAdView)
UIViewController.prepareInterstitialAds()
interAdView.addSubview(closeButton)
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
interAdView.removeFromSuperview()
let myScene = GameOver(size: self.size)
myScene.scaleMode = self.scaleMode
let transition = SKTransition.flipHorizontalWithDuration(1.0)
self.view?.presentScene(myScene, transition: transition)
adLoaded = false
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
adLoaded = false
closeButton.removeFromSuperview()
let myScene = GameOver(size: self.size)
myScene.scaleMode = self.scaleMode
let transition = SKTransition.flipHorizontalWithDuration(1.0)
self.view?.presentScene(myScene, transition: transition)
interAdView.removeFromSuperview()
}
func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
// Done with this ad. Lets get a new one
interAdView.removeFromSuperview()
//adLoaded = false
}
}