使用 Swift 在 xcode 6 中使用 iAd
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24771295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iAd in xcode 6 with Swift
提问by Benzene
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 answerin another question about iAd("Swift - ADBannerView") but still couldn't make it.
我正在努力在场景中实现横幅广告,但它总是报告“线程 1:EXC_BREAKPOINT(code=EXC_ARM_BREAKPOINT, subcode=Oxdefe) 并且程序停止运行。我在另一个关于 iAd(的问题)中引用了 T 先生的回答” Swift - ADBannerView”),但仍然无法实现。
The code looks like this:
代码如下所示:
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!) {
}
}
And I created an ADBannerView in the Main.storyboard and linked it with the @IBOutlet adBannerView.
我在 Main.storyboard 中创建了一个 ADBannerView 并将它与@IBOutlet adBannerView 链接起来。
Anyone helps me figure out?
有没有人帮我弄清楚?
回答by erdekhayser
This is how I did it, possibly not all of it is necessary.
我就是这样做的,可能不是所有的都是必要的。
I did not use the banner in the Storyboard, so the IBOutlet is not necessary.
我没有在 Storyboard 中使用横幅,因此不需要 IBOutlet。
Also, if you manually create a banner, you do not need to set self.canDisplayBannerAds
此外,如果您手动创建横幅,则无需设置 self.canDisplayBannerAds
This function (ported from ObjC) is how I display the ads.
这个函数(从 ObjC 移植)是我展示广告的方式。
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)
}
This is called in viewDidLoad
. Then, in the didLoadAd
delegate method, I set adBannerView.hidden = false
and in didFailToReceiveAdWithError
, adBannerView.hidden = true
这在viewDidLoad
. 然后,在didLoadAd
委托方法中,我设置adBannerView.hidden = false
并在didFailToReceiveAdWithError
,adBannerView.hidden = true
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).
我认为hidden
比alpha
在这种情况下更好,因为它感觉更自然。我相信(但不确定)隐藏时,GPU 根本不会绘制视图,而当 alpha 为 0 时,它仍会被绘制,但不可见(如果我错了,请纠正我)。
This is my setup, and it worked for me, so hopefully it will work in your case too!
这是我的设置,它对我有用,所以希望它也适用于你的情况!