How to display iAd banner in different SpriteKit scripts

I made a simple game with Swift and SpriteKit. Everything seems to work, but I have a problem with my iAd installation. I want to show the banner ad only in certain scenes (main menu and game) not during the game. My iAd setting works, but only if it is displayed constantly. My last attempt to fix this was with the NSNotificationCenter method. I did it as I saw it in other questions / answers here, but the application crashes right after launch. Hope someone can help me. Just let me know if you need more of my code. Thanks in advance.

GameViewController.swift

class GameViewController: UIViewController, ADBannerViewDelegate { var adBannerView = ADBannerView(frame: CGRect.zeroRect) override func viewDidLoad() { super.viewDidLoad() NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "hideAd", object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleNotification", name: "showAd", object: nil) self.adBannerView.delegate = self self.adBannerView.hidden = true adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2) view.addSubview(adBannerView) if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene { // Configure the view. let skView = self.view as SKView /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true var scene: SKScene = MainMenu(size: skView.bounds.size) scene.scaleMode = SKSceneScaleMode.AspectFill skView.presentScene(scene) } } func handleNotification(notification: NSNotification){ if notification.name == "hideAd"{ adBannerView.hidden = true }else if notification.name == "showAd"{ adBannerView.hidden = false } } //iAD Setup func bannerViewWillLoadAd(banner: ADBannerView!) { println("Ad loads") } func bannerViewDidLoadAd(banner: ADBannerView!){ println("Ad loaded") self.adBannerView.hidden = false } func bannerViewActionDidFinish(banner: ADBannerView!) { println("resume scene") let skView = self.view as SKView skView.paused = false } func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool { println("pause scene") let skView = self.view as SKView skView.paused = true return true } func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) { println("Failed to load ad") self.adBannerView.hidden = true } more code.... 

MainMenu.swift

 class MainMenu: SKScene{ required init(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } override init(size:CGSize){ super.init(size: size) NSNotificationCenter.defaultCenter().postNotificationName("showAd", object: nil) more code... 
+5
source share
2 answers

It works for me

In my controller, I have this code:

// Banner ad

 var SH = UIScreen.mainScreen().bounds.height let transition = SKTransition.fadeWithDuration(1) var UIiAd: ADBannerView = ADBannerView() override func viewWillAppear(animated: Bool) { var BV = UIiAd.bounds.height UIiAd.delegate = self UIiAd.frame = CGRectMake(0, SH + BV, 0, 0) self.view.addSubview(UIiAd) } override func viewWillDisappear(animated: Bool) { UIiAd.delegate = nil UIiAd.removeFromSuperview() } func bannerViewDidLoadAd(banner: ADBannerView!) { var BV = UIiAd.bounds.height UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1) // Time it takes the animation to complete UIiAd.alpha = 1 // Fade in the animation UIView.commitAnimations() } func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) { UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1) UIiAd.alpha = 0 UIView.commitAnimations() } func showBannerAd() { UIiAd.hidden = false var BV = UIiAd.bounds.height UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1) // Time it takes the animation to complete UIiAd.frame = CGRectMake(0, SH - BV, 0, 0) // End position of the animation UIView.commitAnimations() } func hideBannerAd() { UIiAd.hidden = true var BV = UIiAd.bounds.height UIView.beginAnimations(nil, context: nil) UIView.setAnimationDuration(1) // Time it takes the animation to complete UIiAd.frame = CGRectMake(0, SH + BV, 0, 0) // End position of the animation UIView.commitAnimations() } override func viewDidLoad() { super.viewDidLoad() self.UIiAd.hidden = true self.UIiAd.alpha = 0 NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideBannerAd", name: "hideadsID", object: nil) NSNotificationCenter.defaultCenter().addObserver(self, selector: "showBannerAd", name: "showadsID", object: nil) let scene = GameScene() // Configure the view. let skView = self.view as SKView skView.showsFPS = false skView.showsNodeCount = false /* Sprite Kit applies additional optimizations to improve rendering performance */ skView.ignoresSiblingOrder = true /* Set the scale mode to scale to fit the window */ scene.scaleMode = .AspectFit scene.anchorPoint = CGPoint(x: 0.5, y: 0.5) scene.size = skView.bounds.size skView.presentScene(scene, transition: transition) } 

To display it in the scene I want (for me, this is a GameOver scene), I did this:

 override func didMoveToView(view: SKView) { showAds() // Rest of your code here... } func showAds(){ NSNotificationCenter.defaultCenter().postNotificationName("showadsID", object: nil) } 

Basically, this code creates a banner from the scene, and when the banner is loaded and the script is called, it slides from below. When you switch scenes, it shifts it again, and when you return to this scene, it slides again from below. If the banner does not load, it turns off, so do not worry about it, showing a white bar. In addition, it sets the alpha value to 0 if it does not load just in case (makes it invisible). Hope this helps.

+8
source

I USE THIS JOIN iAd Banner in subview

var vc = self.view? .window? .rootViewController VK? .view.addSubview (AdView!)

When I want to hide it when in the game (because iAd reduces FPS), I call

AdView! .RemoveFromSuperview ()

When the user has finished the game, just add

  var vc = self.view?.window?.rootViewController vc?.view.addSubview(adView!) 

to show iAd banner again

BUT YOU NEED TO USE THIS CODE IN GameViewController

class GameViewController: ADBannerViewDelegate (you also needed to) also remember to import the iAd at the top of the game controller also import the iAd infrastructure into the project

 override func viewDidLoad() { self.ShowAd() 

// This one also has doLoad

  func ShowAd() { super.viewDidLoad() adView = ADBannerView(adType: ADAdType.Banner) adView!.delegate = self } func bannerViewDidLoadAd(banner: ADBannerView!) { self.view.addSubview(adView!) self.view.layoutIfNeeded() println("Pass") } func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) { adView!.removeFromSuperview() /*var alert = UIAlertController(title: "iAd Message", message: "iAd Fail To Load", preferredStyle: UIAlertControllerStyle.Alert) self.presentViewController(alert, animated: false, completion: nil) alert.addAction(UIAlertAction(title: "dismiss", style: UIAlertActionStyle.Default, handler: nil)) self.view.layoutIfNeeded()*/ println("fail") } 
+1
source

Source: https://habr.com/ru/post/1210509/


All Articles