1

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
    }
}
tktsubota
  • 8,983
  • 3
  • 28
  • 39
Kobi Greene
  • 98
  • 1
  • 9
  • 1
    put loadAd() into a if condition and if condition check the internet connectivity. if true loadAd else not. – Avinash Tag Dec 30 '15 at 04:36
  • How would you check the connectivity? – Kobi Greene Dec 30 '15 at 05:56
  • [Avinash Tag](http://stackoverflow.com/users/1308160/avinash-tag)'s method is unsafe as the user may very well be connected to the internet but the device still hasn't retrieved an ad from the server yet. After an ad unloads, it takes a while before a new ad can be displayed. Worse yet, a user might be connected to a really bad internet connection or the iAd CDN might be down, so you'll still see the user as connected to the internet but still not be able to display an advertisement. – Zachary Espiritu Jan 01 '16 at 18:12

2 Answers2

0

While Avinash Tag's method might work, a significantly easier and, more importantly, safer (more on that below) way to solve your problem is to simply check if interAd.loaded is true before calling your loadAd() method.

You should wrap your code in your didMoveToView() method as such:

override func didMoveToView(view: SKView) {

    if interAd.loaded { // Check if ad is loaded

        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)
    }
    else {
        print("Interstitial not loaded yet!")
    }
}

The reason why this method is much safer than checking for internet connectivity is that the user may very well be connected to the internet but the device still hasn't retrieved an ad from the server yet. After an ad unloads, it takes a while before a new ad can be displayed. Worse yet, a user might be connected to a really bad internet connection or the iAd CDN might be down, so you'll still see the user as connected to the internet but still not be able to display an advertisement.

Community
  • 1
  • 1
Zachary Espiritu
  • 876
  • 7
  • 23
-1

Please see this link about checking internet connectivity on iOS: Easiest way to detect Internet connection on iOS?

Put loadAd() into a if statement check if the user is connected to the internet.

Community
  • 1
  • 1
Avinash Tag
  • 153
  • 2
  • 11