【发布时间】:2015-09-27 09:41:22
【问题描述】:
我想在我的 Spritekit 游戏中实现 iAd 插页式广告,但添加它时遇到了麻烦。我就是这样做的:
import iAd
class GameViewController: UIViewController, ADInterstitialAdDelegate {
var interAd:ADInterstitialAd?
var interAdView = UIView()
var closeButton = UIButton.buttonWithType(UIButtonType.System) as! UIButton
override func viewDidLoad() {
super.viewDidLoad()
closeButton.frame = CGRectMake(20, 20, 70, 44)
closeButton.layer.cornerRadius = 10
closeButton.backgroundColor = UIColor.whiteColor()
closeButton.layer.borderColor = UIColor.blackColor().CGColor
closeButton.layer.borderWidth = 1
closeButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
closeButton.addTarget(self, action: "closeAd:", forControlEvents: UIControlEvents.TouchDown)
closeButton.enabled = false
closeButton.setTitle("skip", forState: UIControlState.Normal)
closeButton.enabled = true
closeButton.setNeedsLayout()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAd", name: "showIntAD", object: nil)
func prepareAd() {
println(" --- AD: Try Load ---")
// Attempt to load a new ad:
interAd = ADInterstitialAd()
interAd?.delegate = self
}
func showAd() -> Bool {
if interAd != nil && interAd!.loaded {
interAdView = UIView()
interAdView.frame = self.view!.bounds
self.view?.addSubview(interAdView)
interAd!.presentInView(interAdView)
UIViewController.prepareInterstitialAds()
interAdView.addSubview(closeButton)
}
return interAd?.loaded ?? false
}
func closeAd(sender: UIButton) {
adFinished()
}
func adFinished() {
closeButton.removeFromSuperview()
interAdView.removeFromSuperview()
}
func interstitialAdDidLoad(interstitialAd: ADInterstitialAd!) {
println(" --- AD: Load Success ---")
}
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
println(" --- AD: Unload --- ")
}
func interstitialAdActionDidFinish(interstitialAd: ADInterstitialAd!) {
println(" --- ADD: Action Finished --- ")
adFinished()
}
func interstitialAdActionShouldBegin(interstitialAd: ADInterstitialAd!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
println(" --- AD: Error --- ")
println(error.localizedDescription)
}
}
我正在尝试在游戏场景中召唤 iAd:
class GameScene: SKScene, SKPhysicsContactDelegate, ADInterstitialAdDelegate {
func showAds(){
NSNotificationCenter.defaultCenter().postNotificationName("showIntAD", object: nil)
}
//Those two are required by Apple to conform "AdInterstitial" protocol
func interstitialAdDidUnload(interstitialAd: ADInterstitialAd!) {
println(" --- AD: Unload --- ")
}
func interstitialAd(interstitialAd: ADInterstitialAd!, didFailWithError error: NSError!) {
println(" --- AD: Error --- ")
println(error.localizedDescription)
}
}
我有一个 testButton,如果我点击它应该会显示一个广告。它什么也没显示!请帮助我,我正在尝试这样做将近一个星期!
【问题讨论】:
标签: swift sprite-kit iad