Interstitial ads from iAd aren't working on swift

﹥>﹥吖頭↗ 提交于 2019-12-24 13:25:20

问题


I'm trying to implement interstitial ads however my code doesn't seem to work.

class ViewController: UIViewController, ADInterstitialAdDelegate {

var interAd = ADInterstitialAd()
var interAdView: UIView = UIView()
var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton

override func viewDidAppear(animated: Bool) {

closeButton.frame = CGRectMake(10, 10, 20, 20)
    closeButton.layer.cornerRadius = 10
    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
    closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown)
loadAd()
}
func close(sender: UIButton) {
    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()
}
func loadAd() {
    println("load ad")
    interAd = ADInterstitialAd()
    interAd.delegate = self
}

func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
    println("ad did load")

    interAdView = UIView()
    interAdView.frame = self.view.bounds
    view.addSubview(interAdView)

    interAd.presentInView(interAdView)
    UIViewController.prepareInterstitialAds()

    interAdView.addSubview(closeButton)
}

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {

}

func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
    println("failed to receive")
    println(error.localizedDescription)

    closeButton.removeFromSuperview()
    interAdView.removeFromSuperview()

}
}

ads aren't showing up, for some reason the app is even tracing "ad did load" but the ad doesn't show up. Sometimes I get the error where xCode tells me that more than 10 instances of ads are being shown at once, yet I haven't been presented with even one ad. What could be the issue? Also it'd be a plus if somebody can tell me how I can make the ad show up after the level ends, but if you can't its all good. Thank you for your help!


回答1:


I created a new SKScene and added this into the file:

class Advertisement11: SKScene, ADInterstitialAdDelegate {
    var interAd = ADInterstitialAd()
    var interAdView = UIView()
    var closeButton = UIButton()
    var returnToScene = SKScene()

    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
        closeButton.addTarget(self, action: "close:", forControlEvents: UIControlEvents.TouchDown)
        loadAd()
    }

    func close(sender: UIButton) {
        closeButton.removeFromSuperview()
        interAdView.removeFromSuperview()
        let block = SKAction.runBlock {
            self.view?.presentScene(self.returnToScene)
        }
        self.runAction(block)//this is to go back to the previous Scene
    }

    func loadAd() {
        print("load ad")
        interAd = ADInterstitialAd()
        interAd.delegate = self
    }

    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
        print("ad did load")

        interAdView = UIView()
        interAdView.frame = self.view!.frame
        view!.addSubview(interAdView)

        interAd.presentInView(interAdView)
        UIViewController.prepareInterstitialAds()

        interAdView.addSubview(closeButton)
    }

    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {

    }

    func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
        print("failed to receive")
        print(error.localizedDescription)

        closeButton.removeFromSuperview()
    //  interAdView.removeFromSuperview()
    }
}


来源:https://stackoverflow.com/questions/31839488/interstitial-ads-from-iad-arent-working-on-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!