如何在不同的SpriteKit场景中显示iAd横幅

时间:2015-01-06 22:03:55

标签: ios swift sprite-kit iad

我用Swift和SpriteKit做了简单的游戏。一切似乎都有效,但我的iAd设置有问题。我只想在游戏过程中不在特定场景(主菜单和游戏结束)中显示广告横幅。 我的iAd设置可以正常工作,但前提是它始终显示。 我最后一次修复它的尝试是使用NSNotificationCenter方法。我已经完成了,因为我已经在其他问题/答案中看到了它,但应用程序在发布后立即崩溃。 希望有人能帮助我。如果您需要更多我的代码,请告诉我。 提前谢谢。

GameViewController.swift

class GameViewController: UIViewController, ADBannerViewDelegate {

var adBannerView = ADBannerView(frame: CGRect.zeroRect)

override func viewDidLoad() {
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "hideAd", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "showAd", object: nil)

    self.adBannerView.delegate = self
    self.adBannerView.hidden = true
    adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
    view.addSubview(adBannerView)

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        // Configure the view.
        let skView = self.view as SKView

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

        var scene: SKScene = MainMenu(size: skView.bounds.size)
        scene.scaleMode = SKSceneScaleMode.AspectFill
        skView.presentScene(scene)
    }
}

func handleNotification(notification: NSNotification){
    if notification.name == "hideAd"{
        adBannerView.hidden = true
    }else if notification.name == "showAd"{
        adBannerView.hidden = false
    }
}

//iAD Setup
func bannerViewWillLoadAd(banner: ADBannerView!) {
    println("Ad loads")
}

func bannerViewDidLoadAd(banner: ADBannerView!){
    println("Ad loaded")
    self.adBannerView.hidden = false
}

func bannerViewActionDidFinish(banner: ADBannerView!) {
    println("resume scene")
    let skView = self.view as SKView
    skView.paused = false
}

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
    println("pause scene")
    let skView = self.view as SKView
    skView.paused = true
    return true
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    println("Failed to load ad")
    self.adBannerView.hidden = true
}   more code.... 

MainMenu.swift

class MainMenu: SKScene{

required init(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

override init(size:CGSize){
    super.init(size: size)

    NSNotificationCenter.defaultCenter().postNotificationName("showAd", object: nil)
more code...

2 个答案:

答案 0 :(得分:8)

这对我有用

在我的视图控制器中,我有以下代码:

//横幅广告

var SH = UIScreen.mainScreen().bounds.height
let transition = SKTransition.fadeWithDuration(1) 
var UIiAd: ADBannerView = ADBannerView()

override func viewWillAppear(animated: Bool) {
    var 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!) {
    var BV = UIiAd.bounds.height
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1) // 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(1)
    UIiAd.alpha = 0
    UIView.commitAnimations()
}

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

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH - BV, 0, 0) // End position of the animation
    UIView.commitAnimations()
}

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

    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1) // Time it takes the animation to complete
    UIiAd.frame = CGRectMake(0, SH + BV, 0, 0) // End position of the animation
    UIView.commitAnimations()
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.UIiAd.hidden = true
    self.UIiAd.alpha = 0

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideBannerAd", name: "hideadsID", object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "showBannerAd", name: "showadsID", object: nil)


    let scene = GameScene()
    // 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.5, y: 0.5)
    scene.size = skView.bounds.size
    skView.presentScene(scene, transition: transition)
}

要在我想要的场景中显示它(对我而言,它是GameOver场景)我这样做了:

override func didMoveToView(view: SKView) {

    showAds()
    // Rest of your code here...
}

func showAds(){
    NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil)
}

基本上,此代码会在场景中创建一个横幅,当加载横幅并调用场景时,它会从底部向上滑动。当你切换场景时,它会再次偏移它,当你回到那个场景时,它会再次从底部向上滑动。如果横幅没有加载,它就会关闭屏幕,所以不用担心它会显示白条。此外,它将alpha设置为0,如果它不加载以防万一(使其不可见)。希望这会有所帮助。

答案 1 :(得分:1)

我使用它来在子视图中展示iAd Banner

var vc = self.view?.window?.rootViewController             VC?.view.addSubview(AD浏览!)

当我想在游戏中隐藏它时(因为iAd减少FPS)我打电话

AD浏览!.removeFromSuperview()

当用户完成游戏时,只需添加

         var vc = self.view?.window?.rootViewController
        vc?.view.addSubview(adView!)

再次显示iAd横幅

但是你需要在GameViewController中实现这个代码

类GameViewController:ADBannerViewDelegate(你也需要这个) 也不要忘记在gameviewcontroller的顶部导入iAd 还将iAd框架导入项目

override func viewDidLoad() {
    self.ShowAd()

//在视图中也有这个载荷

     func ShowAd() {
        super.viewDidLoad()
        adView = ADBannerView(adType: ADAdType.Banner)
        adView!.delegate = self

    }
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        self.view.addSubview(adView!)
        self.view.layoutIfNeeded()
        println("Pass")
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        adView!.removeFromSuperview()
        /*var alert = UIAlertController(title: "iAd Message", message:
            "iAd Fail To Load", preferredStyle: UIAlertControllerStyle.Alert)

        self.presentViewController(alert, animated: false, completion: nil)

        alert.addAction(UIAlertAction(title: "dismiss", style: UIAlertActionStyle.Default,
            handler: nil))
        self.view.layoutIfNeeded()*/
        println("fail")
    }