8

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.

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.

Anyone helps me figure out?

Community
  • 1
  • 1
Benzene
  • 161
  • 1
  • 9

1 Answers1

14

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.

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

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)
}

This is called in viewDidLoad. Then, in the didLoadAd delegate method, I set adBannerView.hidden = false and in 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).

This is my setup, and it worked for me, so hopefully it will work in your case too!

erdekhayser
  • 6,537
  • 2
  • 37
  • 69
  • I tried to delete the line self.canDisplayBannerAds, and it worked! for the hidden/alpha, I'm not sure which is better. Thanks a lot! – Benzene Jul 16 '14 at 22:50
  • I was trying to pause the game while I was interacting with the ad, which is indeed a must. I tried a lot of methods for hours but in vain. The best I could think of is simply put GameScene().paused = true in func bannerViewActionShouldBegin. But it never paused. – Benzene Jul 17 '14 at 01:12
  • 1
    @Benzene That is why I did not use `canDisplayBannerAds`. It doesn't let you have this custom support – erdekhayser Jul 17 '14 at 01:13
  • Yes I did delete that line, and that's why the ad could show up now. It didn't solve the pause problem. It's so frustrating. – Benzene Jul 17 '14 at 01:15
  • There should be a way for you to pause the game when using that option but I contacted Apple tech support, no way to do it :| – erdekhayser Jul 17 '14 at 01:16
  • I contacted them but still no response yet. I'm trying your method now. But it still won't pause at all. The scene is SKScene named GameScene, is it the right way to pause it with GameScene().paused = true ? When I was debugging, all lines were executed but it simply won't pause. – Benzene Jul 17 '14 at 01:20
  • 1
    No, it would be gameScene.paused = true. If you do GameScene().paused, you are creating an all-new scene. That is not what you want. Set the stored scene to be paused instead. – erdekhayser Jul 17 '14 at 01:21
  • That makes sense! But if I use GameScene.paused = true, it reports error as "GameScene.Type does not have a member named'paused'" It's so wierd! – Benzene Jul 17 '14 at 01:24
  • 1
    Do you have a property stored that is an instance of GameScene – erdekhayser Jul 17 '14 at 01:24
  • You are right! Well...I put var gameScene = GameScene() in the GameScene class.When I put gameScene.paused = true, it reports " Use of unresolved identifier 'gameScene'". Then I realized that I'd put it in the GameViewController and I did. No errors after that, but it still won't pause. – Benzene Jul 17 '14 at 01:49
  • What is the line where you initialize the scene? – erdekhayser Jul 17 '14 at 01:51
  • I simply put init(size: CGSize){ super.init(size: self.size) } Does that matter? – Benzene Jul 17 '14 at 01:57
  • No I mean where you set a variable equal to GameScene() – erdekhayser Jul 17 '14 at 01:59
  • To not worry about the size, I deleted the init line in GameScene and the line to set it is "let gameScene = GameScene() " in the GameViewController class. – Benzene Jul 17 '14 at 02:04
  • 1
    You need to store the scene as a property. Like, `class MyViewController{ var gameScene = GameScene() ...rest of class }` – erdekhayser Jul 17 '14 at 02:06
  • Yes. What I have is class GameViewController{var gameScene = GameScene() .... } It seems the same to me. – Benzene Jul 17 '14 at 02:11
  • 1
    Then, instead of when you say `let gameScene = GameScene()` take away the let – erdekhayser Jul 17 '14 at 02:12
  • Yep. I did changed that now. But it still doesn't pause, although no errors reported. The code to pause it is `gameScene.paused = true` in `func bannerViewActionShouldBegin` – Benzene Jul 17 '14 at 02:17
  • Might be worth asking a second question for this part of your issue – erdekhayser Jul 17 '14 at 02:38
  • I am getting ad loaded via delegates but ad is not displaying. – Rohit Goyal Sep 09 '14 at 12:00
  • Your load function needs () – ericgu Mar 17 '15 at 19:41
  • @ericgu nice catch. Edit made. – erdekhayser Mar 17 '15 at 19:44
  • You should also consider making hidden = false for new users – ericgu Mar 17 '15 at 19:48
  • @ericgu You mean to put that line inside the load function? That wouldn't really make sense, because at that point, no ad is loaded. – erdekhayser Mar 17 '15 at 19:56
  • @erdekhayser. Thanks for the prompt response. I mispoke as it was not clear what was going on. I think you may have to use super.addView(adBannerView) instead of self.addView(adBannerView) – ericgu Mar 17 '15 at 20:18