Xcode 如何在Sprite套件Swift中实施间隙iAd

Xcode 如何在Sprite套件Swift中实施间隙iAd,xcode,swift,Xcode,Swift,我想知道是否有人可以告诉我如何做以下事情 我使用Swift和Sprite工具包在Xcode 6.1中创建了一个游戏。我试图实现iAd的间隙广告,每次“游戏场景”被提出。任何帮助都将不胜感激,因为我不知道如何使用雪碧套件做到这一点 我确实找到了一个链接,他们建议这样做,但是当我实现它时,它工作了,但是禁用了我的用户界面和在应用关闭后使用应用的能力。它不会使应用程序崩溃,只是在间隙关闭后,看起来一切都被禁用了: //adding iAd framework import iAd //conform

我想知道是否有人可以告诉我如何做以下事情

我使用Swift和Sprite工具包在Xcode 6.1中创建了一个游戏。我试图实现iAd的间隙广告,每次“游戏场景”被提出。任何帮助都将不胜感激,因为我不知道如何使用雪碧套件做到这一点

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

//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

}
然后又有人说把这个添加到我的场景中就行了:

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

我找到了解决这个问题的方法。您需要将以下行添加到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()
    }
}
然后在游戏代码的某个地方,在其中一个场景中,执行以下操作:

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)
}

我有同样的问题,当iAd被解除时,会留下一个空白显示。永远不会调用InterstitularAdactionDidFinish()和InterstitularAddDunload()函数。