如何在 Sprite Kit Swift 中实现 Interstitial iAd [英] How to implement Interstitial iAd in Sprite Kit Swift

查看:31
本文介绍了如何在 Sprite Kit Swift 中实现 Interstitial iAd的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以告诉我如何执行以下操作.

I was wondering if someone could show me how to do the following.

我使用 Swift 和 Sprite Kit 在 Xcode 6.1 中创建了一个游戏.每次出现GameOverScene"时,我都试图实现 iAd 的插页式广告.任何帮助将不胜感激,因为我不知道如何使用 Sprite Kit 做到这一点.

I created a game in Xcode 6.1 using Swift and Sprite Kit. I am trying to implement iAd's interstitial ad every time the "GameOverScene" is being presented. Any help would be much appreciated as I have no clue how to do this using Sprite Kit.

我确实找到了一个链接,他们建议这样做,但是当我实现它时,它可以工作,但是禁用我的用户界面和应用程序关闭后使用应用程序的能力.它不会使应用程序崩溃,只是在插页式广告关闭后看起来一切都被禁用了:

I did found a link where they suggested doing it like this, however when I implement it works, but disable's my user interface and the ability the use the app after the app closes. It does not crash the app, it just looks like everything is disabled after the interstitial is closed:

//adding iAd framework

import iAd
//conform iAd delegate

class ViewController: UIViewController,ADInterstitialAdDelegate 


//create instance variable
var interstitial:ADInterstitialAd!
//default iAd interstitials does not provide close button so we need to     create one manually

var placeHolderView:UIView!
var closeButton:UIButton!



override func viewDidLoad() {
super.viewDidLoad()

//iAD interstitial
NSNotificationCenter.defaultCenter().addObserver(self, selector: ("runAd:"),    name:UIApplicationWillEnterForegroundNotification, object: nil)

}




//iAD interstitial
func runAd(notification:NSNotification){
var timer = NSTimer.scheduledTimerWithTimeInterval(3.0, target: self,     selector: Selector("dislayiAdInterstitial"), userInfo: nil, repeats: false)
cycleInterstitial()
}

func cycleInterstitial(){

// Clean up the old interstitial...
//  interstitial.delegate = nil;
// and create a new interstitial. We set the delegate so that we can be notified of when
interstitial = ADInterstitialAd()
interstitial.delegate = self;
}

func presentInterlude(){
// If the interstitial managed to load, then we'll present it now.
if (interstitial.loaded) {

    placeHolderView = UIView(frame: self.view.frame)
    self.view.addSubview(placeHolderView)

    closeButton = UIButton(frame: CGRect(x: 270, y:  25, width: 25, height: 25))
    closeButton.setBackgroundImage(UIImage(named: "error"), forState: UIControlState.Normal)
    closeButton.addTarget(self, action: Selector("close"), forControlEvents: UIControlEvents.TouchDown)
    self.view.addSubview(closeButton)


    interstitial.presentInView(placeHolderView)
}
}

// iAd Delegate Mehtods

// When this method is invoked, the application should remove the view from the screen and tear it down.
// The content will be unloaded shortly after this method is called and no new content will be loaded in that view.
// This may occur either when the user dismisses the interstitial view via the dismiss button or
// if the content in the view has expired.

func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!){
placeHolderView.removeFromSuperview()
closeButton.removeFromSuperview()
interstitial = nil

cycleInterstitial()
}


func interstitialAdActionDidFinish(_interstitialAd: ADInterstitialAd!){
placeHolderView.removeFromSuperview()
closeButton.removeFromSuperview()
interstitial = nil

println("called just before dismissing - action finished")

}

// This method will be invoked when an error has occurred attempting to get advertisement content.
// The ADError enum lists the possible error codes.
func interstitialAd(interstitialAd: ADInterstitialAd!,
didFailWithError error: NSError!){
    cycleInterstitial()
}


//Load iAd interstitial
func dislayiAdInterstitial() {
//iAd interstitial
presentInterlude()
}


func close() {
placeHolderView.removeFromSuperview()
closeButton.removeFromSuperview()
interstitial = nil

}

然后又有人说将它添加到我的场景中就可以了:

And then again someone said add this to my scene would just do it :

func fullScreenAd() {
if self.requestInterstitialAdPresentation() {
    println("ad loaded")
}
}

推荐答案

我找到了解决方法.您需要将这些行添加到您的 ViewController 中:

I found out how to solve this. You need to add these lines to your ViewController:

class GameViewController: UIViewController, ADInterstitialAdDelegate {

   var interstitialAd:ADInterstitialAd!
   var interstitialAdView: UIView = UIView()
   // make myScene the SKScene you want to display after the ad goes away
   var myScene: SKScene?


   override func viewDidLoad() {
       super.viewDidLoad()

    // prepare ads
    UIViewController.prepareInterstitialAds()
    self.interstitialPresentationPolicy = ADInterstitialPresentationPolicy.None
    loadInterstitialAd()

   }

// MARK: interstitial add support functions

    func showAd() {
        interstitialAdView = UIView()
        interstitialAdView.frame = self.view.bounds
        view.addSubview(interstitialAdView)

        interstitialAd.presentInView(interstitialAdView)
        UIViewController.prepareInterstitialAds()
    }

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

    func interstitialAdWillLoad(interstitialAd: ADInterstitialAd!) {
        print("AdWillLoad")
    }

    func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
        print("AdDidLoad")
        /*
        showAd()
        */
    }

    func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
        print("AdActionDidFinish")
        interstitialAdView.removeFromSuperview()
        let skView = self.view as! SKView
        skView.presentScene(myScene!)
    }

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

    func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
        print("AdDidFailWithError")
        interstitialAdView.removeFromSuperview()
        UIViewController.prepareInterstitialAds()
    }

    func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
        print("AdDidUnload")
        interstitialAdView.removeFromSuperview()
        UIViewController.prepareInterstitialAds()
    }
}

然后在您的游戏代码中的某个场景中,执行以下操作:

Then somewhere in your game code, in one of the scenes, do this:

let appDelegate  = UIApplication.sharedApplication().delegate as! AppDelegate
let viewController = appDelegate.window!.rootViewController as! GameViewController

if viewController.interstitialAd.loaded {
    viewController.showAd()
} else {
    print("Ad Not Loaded")
    // scene change if no ad ready to be shown
    self.scene!.view?.presentScene(viewController.myScene!, transition: transition)
}

这篇关于如何在 Sprite Kit Swift 中实现 Interstitial iAd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆