IAd in xcode 6 with Swift

I am working on the implementation of an advertising banner in the scene, but it always says "Thread 1: EXC_BREAKPOINT (code = EXC_ARM_BREAKPOINT, subcode = Oxdefe), and the program stops working. I referred to Mr. T's answer in another question about iAd (" Swift - ADBannerView "), but still could not do it.

The code is as follows:

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 ADBannerView in Main.storyboard and linked it to @IBOutlet adBannerView.

Anyone help me figure this out?

+8
xcode swift xcode6 iad adbannerview
source share
1 answer

Here's how I did it; maybe not all of this is necessary.

I did not use the banner in the Storyboard, therefore IBOutlet is not needed.

In addition, if you manually create a banner, you do not need to set self.canDisplayBannerAds

This feature (ported from ObjC) is a way to display 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) } 

This is called in viewDidLoad . Then in the delegate method didLoadAd I set adBannerView.hidden = false and in didFailToReceiveAdWithError , adBannerView.hidden = true

I think hidden better than alpha in this situation, as it seems more natural. I believe (but not sure) that when hidden, the view is not drawn at all by the GPU, but with alpha 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 this will work in your case too!

+14
source share

All Articles