我正在尝试在SpriteKit游戏中设置iAd Banner,因此我以Apple,SpaceShip提供的SpriteKit模板为例。将此示例用作指南Example Swift iAd 我遇到的问题是提供的示例是SpriteKit,因此没有SkScenes交互。因此,试图在GameViewController中复制它对我来说是不可能的。 这是我的代码。
import UIKit
import SpriteKit
import iAd
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
class GameViewController: UIViewController, ADBannerViewDelegate {
var bannerView:ADBannerView?
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
self.bannerView?.delegate = self
self.bannerView?.hidden = true
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.bannerView?.hidden = false
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.bannerView?.hidden = true
}
}
此代码运行但命中BreakPoint,不向控制台输出任何消息。
替代代码
阅读类似的问题我发现这个Stackoverflow Question我试过并且效果更好,加载了游戏场景并加载并显示了iAdBanner,问题是iAd横幅不可点击,因此添加横幅不会加载到全屏。这是我用于此的代码:
import UIKit
import SpriteKit
import iAd
extension SKNode {
class func unarchiveFromFile(file : NSString) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
class GameViewController: UIViewController, ADBannerViewDelegate {
@IBOutlet var adBannerView: ADBannerView?
override func viewDidLoad() {
super.viewDidLoad()
loadAds()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.adBannerView?.hidden = false
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.adBannerView?.hidden = true
}
func loadAds() {
adBannerView = ADBannerView(frame: CGRect.zeroRect)
self.adBannerView?.delegate = self
self.adBannerView?.hidden = true
view.addSubview(adBannerView!)
}
}
谢谢你们的任何帮助都会非常感激。