使用Swift的xcode 6中的iAd [英] iAd in xcode 6 with Swift
问题描述
我正在努力在现场实施横幅广告,但是它始终报告线程1:EXC_BREAKPOINT(代码= EXC_ARM_BREAKPOINT,子代码= Oxdefe),程序停止运行。
我引用了T先生的答案在有关iAd的另一个问题( Swift-ADBannerView)中仍然无法实现。
I'm working to implement a banner ad in the scene, but it always reports "Thread 1: EXC_BREAKPOINT(code=EXC_ARM_BREAKPOINT, subcode=Oxdefe) and the program stops running. I referenced Mr. T's answer in another question about iAd("Swift - ADBannerView") but still couldn't make it.
代码如下:
import UIKit
import SpriteKit
import iAd
class GameViewController: UIViewController, ADBannerViewDelegate {
@IBOutlet var adBannerView: ADBannerView
override func viewDidLoad() {
super.viewDidLoad()
println("view loaded")
//iAd
self.canDisplayBannerAds = true
self.adBannerView.delegate = self
self.adBannerView.alpha = 0.0
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)
}
}
//iAd
func bannerViewWillLoadAd(banner: ADBannerView!) {
println("sort of working1")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.adBannerView.alpha = 1.0
println("sort of working2")
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("sort of working3")
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
println("sort of working4")
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
}
}
然后我在Main.storyboard中创建了一个ADBannerView并将其与@IBOutlet adBannerView链接。
And I created an ADBannerView in the Main.storyboard and linked it with the @IBOutlet adBannerView.
有人帮我弄清楚吗?
推荐答案
这就是我的操作方式,可能不是全部都是必要的。
This is how I did it, possibly not all of it is necessary.
我没有在情节提要中使用横幅,因此没有必要使用IBOutlet。
I did not use the banner in the Storyboard, so the IBOutlet is not necessary.
此外,如果您手动创建横幅,则无需设置 self.canDisplayBannerAds
Also, if you manually create a banner, you do not need to set self.canDisplayBannerAds
此功能(从ObjC移植)我是如何显示广告的。
This function (ported from ObjC) is how I display the ads.
func loadAds(){
adBannerView = ADBannerView(frame: CGRect.zeroRect)
adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
adBannerView.delegate = self
adBannerView.hidden = true
view.addSubview(adBannerView)
}
这在 viewDidLoad
中调用。然后,在 didLoadAd
委托方法中,设置 adBannerView.hidden = false
和 didFailToReceiveAdWithError
, adBannerView.hidden = true
This is called in viewDidLoad
. Then, in the didLoadAd
delegate method, I set adBannerView.hidden = false
and in didFailToReceiveAdWithError
, adBannerView.hidden = true
我认为隐藏了在这种情况下
比 alpha
更好,因为它感觉更自然。我相信(但不确定)隐藏时,GPU根本不会绘制视图,而Alpha为0时,它仍然被绘制,但变为不可见(如果我错了,请纠正我)。
I think hidden
is better than alpha
in this situation, as it feels more natural. I believe (but am not sure) that when hidden, the view is not drawn at all by the GPU, while with an alpha of 0, it is still drawn, but made invisible (correct me if I am wrong).
这是我的设置,对我有用,所以希望它也能在您的情况下使用!
This is my setup, and it worked for me, so hopefully it will work in your case too!
这篇关于使用Swift的xcode 6中的iAd的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!