iAd 插屏呈现延迟
iAd interstitial presentation is delayed
我使用 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
}
您在 viewDidLoad
中注释掉了 //loadInterstitialAd()
。取消注释。您需要尽早加载插页式广告,以便在您决定展示它时它已完全加载并准备好显示。
此外,您在 func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!)
加载插页式广告后立即展示它。您应该创建一个功能或操作来在您需要时展示您的插页式广告,例如当用户执行特定操作或游戏结束时。
检查我的回答here。点击我创建的 UIButton
@IBAction func presentAdButton
后,就会显示插页式广告。
我使用 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
}
您在 viewDidLoad
中注释掉了 //loadInterstitialAd()
。取消注释。您需要尽早加载插页式广告,以便在您决定展示它时它已完全加载并准备好显示。
此外,您在 func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!)
加载插页式广告后立即展示它。您应该创建一个功能或操作来在您需要时展示您的插页式广告,例如当用户执行特定操作或游戏结束时。
检查我的回答here。点击我创建的 UIButton
@IBAction func presentAdButton
后,就会显示插页式广告。