支持 iAd 的 Spritekit

Spritekit Supporting iAd

我一直在网上到处寻找有关如何在我的 spritekit 游戏中包含 iad 横幅的教程。例如,我查看了这个: 但我遇到了一些错误,可能是由于新的 swift 2 和 swift 1。但是你能解释一下如何将 IAD 包含在我的 spritekit 中吗游戏。先感谢您。

确保您的 GameViewController 中有此代码。我刚刚对此进行了测试,它适用于横向和纵向模式。

 class GameViewController: UIViewController, ADBannerViewDelegate {

//--------------------
//-----iAd Banner-----
//--------------------
var SH = UIScreen.mainScreen().bounds.height
let transition = SKTransition.fadeWithDuration(0)
var UIiAd: ADBannerView = ADBannerView()

override func viewWillAppear(animated: Bool) {
    let BV = UIiAd.bounds.height
    UIiAd.delegate = self
    UIiAd.frame = CGRectMake(0, SH + BV, 0, 0)
    self.view.addSubview(UIiAd)
}

override func viewWillDisappear(animated: Bool) {
    UIiAd.delegate = nil
    UIiAd.removeFromSuperview()
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
    UIiAd.alpha = 1 // Fade in the animation
    UIView.commitAnimations()
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.5)
    UIiAd.alpha = 0
    UIView.commitAnimations()
}

func showBannerAd() {
    UIiAd.hidden = false
    let BV = UIiAd.bounds.height

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH - BV, UIScreen.mainScreen().bounds.width, 0)
    UIView.commitAnimations()
}

func hideBannerAd() {
    UIiAd.hidden = true
    let BV = UIiAd.bounds.height

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(0.5) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH + BV, UIScreen.mainScreen().bounds.width, 0) //Beginning position of the ad
    UIView.commitAnimations()
}



//--------------------
//-----View Loads-----
//--------------------
override func viewDidLoad() {
    super.viewDidLoad()
    //iAd Control
    self.UIiAd.hidden = true
    self.UIiAd.alpha = 0
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.hideBannerAd), name: "hideadsID", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(GameViewController.showBannerAd), name: "showadsID", object: nil)
}

在 class 声明之外添加以下内容。 class 你去哪里并不重要,只要它是全球性的,这样你就可以从任何游戏场景访问它们。

 //-------------
 //-----Ads-----
 //-------------
 func showAds() {
     NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil)
 }
 func hideAds() {
     NSNotificationCenter.defaultCenter().postNotificationName("hideadsID", object: nil)
 }

然后当你想在特定场景或任何时候显示广告时,只需调用"showAds()"函数或"hideAds()"隐藏它们。