【问题标题】:iAd interstitial presentation is delayediAd 插页式展示延迟
【发布时间】:2015-09-10 15:42:20
【问题描述】:

我使用 Swift 和 Sprite Kit 制作了一款游戏,它会在玩家死亡时加载 iAd 插页式广告。但是,广告有时会延迟,因此在游戏重新启动时会在游戏过程中弹出。有没有办法解决这个问题?我使用以下方法从我的 GameScene 调用 iAd 插页式广告:

NSNotificationCenter.defaultCenter().postNotificationName("showInterstitialAd", object: nil)

GameViewController.swift

import UIKit  
import SpriteKit  
import iAd  

class GameViewController: UIViewController, ADBannerViewDelegate, ADInterstitialAdDelegate {

    var interstitialAd:ADInterstitialAd!
    var interstitialAdView: UIView = UIView()
    let transition = SKTransition.fadeWithDuration(1)

    var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton

    override func viewDidLoad() {
        super.viewDidLoad()

        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)

        //loadInterstitialAd()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "loadInterstitialAd", name: "showInterstitialAd", object: nil)

        let scene = GameScene(size:CGSize(width: 2208, height: 1242))
        // Configure the view.
        let skView = self.view as! SKView
        skView.showsFPS = false
        skView.showsNodeCount = false

        /* Sprite Kit applies additional optimizations to improve rendering performance */
        skView.ignoresSiblingOrder = true

        /* Set the scale mode to scale to fit the window */
        scene.scaleMode = .AspectFit
        scene.anchorPoint = CGPoint(x: 0, y: 0)

        skView.presentScene(scene, transition: transition)
    }

    func close(sender: UIButton) {
        closeButton.removeFromSuperview()
        interstitialAdView.removeFromSuperview()
        interstitialAd.delegate = nil
    }

    func loadInterstitialAd() {
        interstitialAd = ADInterstitialAd()
        interstitialAd.delegate = self
    }

    func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
    }

    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
        interstitialAd.delegate = self
        interstitialAdView = UIView()
        interstitialAdView.frame = self.view.bounds
        view.addSubview(interstitialAdView)
        view.addSubview(closeButton)
        interstitialAd.presentInView(interstitialAdView)
        UIViewController.prepareInterstitialAds()
    }

    func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
        closeButton.removeFromSuperview()
        interstitialAdView.removeFromSuperview()
    }

    func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
        return true
    }

    func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
        interstitialAdView.removeFromSuperview()
        closeButton.removeFromSuperview()
        interstitialAd.delegate = nil
    }

    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
        closeButton.removeFromSuperview()
        interstitialAdView.removeFromSuperview()
        interstitialAd.delegate = nil
    }

【问题讨论】:

  • 是的,几乎可以肯定有办法解决这个问题。如果您愿意分享一些细节,也许我们甚至可以告诉您如何...
  • 请添加您可能拥有的任何相关代码。

标签: swift sprite-kit iad


【解决方案1】:

您已在 viewDidLoad 中注释掉了 //loadInterstitialAd()。取消注释。您需要尽早加载插页式广告,以便在您决定展示它时,它已完全加载并准备好展示。

此外,当您的插页式广告加载到您的 func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) 时,您会立即展示它。您应该创建一个函数或操作,以便在您需要时展示您的插页式广告,例如当用户执行某个操作或游戏结束时。

检查我的答案here。一旦点击我创建的UIButton @IBAction func presentAdButton,就会显示插页式广告。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    相关资源
    最近更新 更多