我想知道是否有人可以告诉我如何执行以下操作。
我使用 Swift 和 Sprite Kit 在 Xcode 6.1 中创建了一个游戏。每次呈现“GameOverScene”时,我都试图实现 iAd 的插页式广告。任何帮助将不胜感激,因为我不知道如何使用 Sprite Kit 执行此操作。
我确实找到了一个链接,他们建议这样做,但是当我实现它时它起作用了,但是禁用了我的用户界面以及在应用程序关闭后使用该应用程序的能力。它不会使应用程序崩溃,只是看起来在插页式广告关闭后一切都被禁用了:
//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)
}
关于xcode - 如何在 Sprite Kit Swift 中实现 Interstitial iAd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29218976/