How to display iAd after the game scene has loaded? - swift

I am confused as to why this doesn't work. I got an error: Thread1:SIGABRT. And yes, I did look at the post about Thread1: SIGABRT but it did not solve my issue.
If someone could please help it would be great. It can be loaded without the iAd banner. But when the iAd code is written in the program, it freezes.
import UIKit
import SpriteKit
import iAd
class GameViewController: UIViewController, ADBannerViewDelegate {
#IBOutlet var Banner: ADBannerView!
var scene: GameScene!
override func viewDidLoad() {
super.viewDidLoad()
Banner.hidden = true
Banner.delegate = self
self.canDisplayBannerAds = true
// Configure the view
let skView = view as! SKView
skView.multipleTouchEnabled = false
// Create and configure the scene
scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
// Present the scenee
skView.presentScene(scene)
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> Int {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
} else {
return Int(UIInterfaceOrientationMask.All.rawValue)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("Error!")
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave:Bool) ->Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!){
Banner.hidden = false
}
}

Here is some working code for you that just worked for me. This does not even need self.candisplaybannerads = true as I had some issues with that. The ad automatically changes the size according to the screen size and is located at the bottom of the screen.
import iAd
class viewController: UIViewController, ADBannerViewDelegate {
var AdBanner = ADBannerView()
override func viewDidLoad() {
super.viewDidLoad()
/* Ad Banner Settings */
AdBanner = ADBannerView()
AdBanner.frame = CGRectZero
AdBanner.delegate = self
self.AdBanner.frame = CGRectMake(0, self.view.frame.size.height-self.AdBanner.frame.size.height, self.AdBanner.frame.size.width, self.AdBanner.frame.size.height)
AdBanner.backgroundColor = UIColor.clearColor()
self.view .addSubview(AdBanner)
}
/* All iAd Functions */
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
/* whatever you need */
return true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
/* whatever you need */
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
AdBanner.hidden = false
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("Error Loading Ad")
/* whatever you need */
AdBanner.hidden = true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
/* whatever you need */
}

Related

iAd banner ad is not working because of the following error (Sprite Kit)

I am using sprite kit and I want the banner ad to display on all of my skscenes. I do not want to use self.candisplaybannerads because it pushes my scene up mid game and causes it to lead to a death in the game.
Here is the error: WARNING: More than 10 instances of ADBannerView or ADInterstitialView currently exist. This is a misuse of the iAd API, and ad performance will suffer as a result. This message is printed only once.
Here is my code:
import UIKit
import iAd
#UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate {
var window: UIWindow?
var AdBanner = ADBannerView()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool {
// Override point for customization after application launch.
UIViewController.prepareInterstitialAds()
/* Ad Banner Settings */
AdBanner = ADBannerView()
AdBanner.frame = CGRectZero
AdBanner.delegate = self
AdBanner.backgroundColor = UIColor.clearColor()
/* All iAd Functions */
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
/* whatever you need */
return true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
/* whatever you need */
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
AdBanner.hidden = false
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("Error Loading Ad")
/* whatever you need */
AdBanner.hidden = true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
/* whatever you need */
}
return true
}
Here is where I am calling the banner ad
class GameViewController: UIViewController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
override func viewDidLoad() {
super.viewDidLoad()
appDelegate.AdBanner.frame = CGRectMake(0, self.view.frame.size.height-appDelegate.AdBanner.frame.size.height, appDelegate.AdBanner.frame.size.width, appDelegate.AdBanner.frame.size.height)
self.view .addSubview(appDelegate.AdBanner)
//authenticateLocalPlayer()
if let scene = StartScreen(fileNamed:"StartScreen") {
// 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 = .AspectFill
skView.presentScene(scene)
}
}
I had the same issue and solved it by using the automatic mode for banner ads. Just set 'canDisplayBannerAds' in your ViewController and Apple takes care about the number of instances:
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
...

iAd banner is fetched but not visible

I'm trying to make iAd banner. Console says that banner is loaded and fetched successfully, but I can't see it in the simulator. What am I doing wrong here?
class GameViewController: UIViewController, ADBannerViewDelegate {
var banner : ADBannerView = ADBannerView()
in viewDidLoad:
{
banner.delegate = self
banner.alpha = 0.0
NSNotificationCenter.defaultCenter().addObserver(self, selector: "bannerViewDidLoadAd", name: "banner", object: nil)
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
println("banner tries to load")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("banner successfully fetched")
UIView.animateWithDuration(0.5, animations: {banner.alpha = 1.0})
UIView.commitAnimations()
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("banner is closed")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("banner failed too! Man...")
println(error.localizedDescription)
UIView.animateWithDuration(0.5, animations: {banner.alpha = 1.0})
}
in GameScene:
func iBanner() {
NSNotificationCenter.defaultCenter().postNotificationName("banner", object: nil)
}
override func didMoveToView(view: SKView) {
iBanner()
}

iAds banner jerking/changing size of scene, every time iAd banner is loaded.

Im having troubles with implementing iAds into game without the iAds banner changing the scene e.g. jerking the screen, Every time the ad is loaded. Please help with a solution to stop this from happining.
class GameViewController: UIViewController,ADBannerViewDelegate{
#IBOutlet var adBannerView: ADBannerView? //connect in IB connection inspector with your ADBannerView
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
var defaultsV = NSUserDefaults.standardUserDefaults();
defaultsV.setInteger(0, forKey: "gameOverCount")
defaultsV.setBool(false, forKey: "firstSessionEnded")
defaultsV.synchronize()
//self.adBannerView!.frame = CGRectMake(0, self.view.frame.size.height-self.adBannerView!.frame.size.height, self.adBannerView!.frame.size.width, self.adBannerView!.frame.size.height)
self.adBannerView!.delegate = self
self.adBannerView!.hidden = true //hide until ad loaded
self.canDisplayBannerAds = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
self.adBannerView?.sizeToFit()
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
NSLog("bannerViewWillLoadAd")
//self.canDisplayBannerAds = true
self.adBannerView!.hidden = true //hide until ad loaded
self.adBannerView!.frame = CGRectMake(0, self.view.frame.size.height-self.adBannerView!.frame.size.height, self.adBannerView!.frame.size.width, self.adBannerView!.frame.size.height)
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
NSLog("bannerViewDidLoadAd")
self.adBannerView!.frame = CGRectMake(0, self.view.frame.size.height-self.adBannerView!.frame.size.height, self.adBannerView!.frame.size.width, self.adBannerView!.frame.size.height)
self.adBannerView!.hidden = false //now show banner as ad is loaded
//self.canDisplayBannerAds = true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
NSLog("bannerViewDidLoadAd")
//optional resume paused game code
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
NSLog("bannerViewActionShouldBegin")
//optional pause game code
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("bannerView")
self.adBannerView!.hidden = true
}
You suggest you should always set canDisplayBannerAds to false as this allows the viewController to resize the view when an ad is shown. If you need create another Bool value to determine the state of your ads.
//ViewControlller.m
self.canDisplayBannerAds = false
use the method for iads banner:
override func viewWillAppear(animated: Bool) {
// View is about to be obscured by an advert.
// Pause activities if necessary
}
override func viewWillDisappear(animated: Bool) {
// Advert has been dismissed. Resume paused activities
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError
error: NSError!) {
banner.removeFromSuperview()
self.view.layoutIfNeeded()
}
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
rectangleAdView = ADBannerView(adType: ADAdType.MediumRectangle)
rectangleAdView?.delegate = self
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.view.addSubview(banner)
self.view.layoutIfNeeded()
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
banner.removeFromSuperview()
self.view.layoutIfNeeded()
}
http://www.techotopia.com/index.php/Integrating_iAds_into_an_iOS_8_App_using_Swift

Implement iAd in Spritekit Game With Swift

I can't seem to be able to load iAd banners in a Swift app within a SpriteKit game... Been googleing it for a while and nothing.... It simply won't make the network call to load the ad... I have implemented all of the necessary calls and set the delegate with no luck...
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("Failed to load ad")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("Loaded Ad")
//now show the ad
//set position off-screen
banner.frame.origin.y = screenHeight
banner.hidden = false
}
func createAds() {
//create ad to allow it to load
let adBanner = ADBannerView(frame: CGRectMake(0, 0, screenWidth, 50))
adBanner.delegate = self
adBanner.center = CGPointMake(screenWidth/2, 25)
adBanner.hidden = true
//add to screen
self.view?.addSubview(adBanner)
println("Created Ad")
}
func hideAds() {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
then I simply call the createAds() method when the game starts which then shows a white rectangle at the top of the screen ("0,0", "quote, quote, is it zero zero a the top"?)
You need to do it like this (in GameViewController.swift):
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("error!")
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
You also need to import iAd
and add ADBannerViewDelegate to the GameViewController class

Pause SpriteKit Scene when iAd is clicked in Swift

I'm implementing banner ads in my game. With the help of #erdekhayser the banner was displayed successfully. But when I tap on the banner, the game won't pause.
The code looks like this:
import UIKit
import SpriteKit
import iAd
import Foundation
class GameViewController: UIViewController, ADBannerViewDelegate{
var gameScene = GameScene()
var adBannerView = ADBannerView(frame: CGRect.zeroRect)
func loadAds() {
adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
adBannerView.delegate = self
view.addSubview(adBannerView)
}
//iAd bannerView
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewDidLoadAd(banner: ADBannerView!){
loadAds()
println("1")
adBannerView.hidden = false //now show banner as ad is loaded
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("2")
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
//Tap to view the ad
gameScene.paused = true
println("3")
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
adBannerView.hidden = true
println("44444")
}
// iAd stopped here ///////////
}
Anyone helps me figure it out?
OK, I solved it myself, maybe not the best way.
I added
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.paused = true
}
in the bannerViewActionShouldBegin method.
Add it worked.