没有互联网连接时删除iAd插页式广告

时间:2015-12-30 01:43:43

标签: ios swift iad ads

当有互联网连接时,我的iAd工作正常,但是当没有互联网时,它会呈现灰色屏幕。当我将模拟器设置为通过切换到下一个屏幕时出错时,我的代码也可以处理。我试图检查广告是否已加载,如果不加载则转换,但会自动转换到下一个屏幕。

我的代码:

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
    }
}

2 个答案:

答案 0 :(得分:0)

虽然Avinash Tag的方法可能会有效,但更简单,更重要的是,更安全(以下更多内容)解决问题的方法是简单地检查{{1在调用interAd.loaded方法之前,这是真的。

您应该将代码包装在loadAd()方法中:

didMoveToView()

这种方法比检查互联网连接更安全的原因是用户可能很好地连接到互联网,但设备仍然没有从服务器检索广告。广告卸载后,需要一段时间才能显示新广告。更糟糕的是,用户可能连接到非常糟糕的互联网连接或者iAd CDN可能已关闭,因此您仍然会看到用户已连接到互联网但仍无法显示广告。

答案 1 :(得分:-1)

请参阅此链接,了解有关检查iOS上的互联网连接的信息:Easiest way to detect Internet connection on iOS?

loadAd()放入if语句,检查用户是否已连接到互联网。