iAd banner is fetched but not visible - swift

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

Related

ADBannerView Issues

I have this code on all my viewcontroller pages to control my AdBanner across multiple pages. It works correctly on the last version on my app but for some reason in this update I'm working on I don't think my app is calling either the bannerViewDidLoadAd or didFailToReceiveAdWithError functions. Also I am getting this error when I change pages a lot. `
"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."
` Is there something I am missing? Thanks
// Ad controller
var UIiAd: ADBannerView = ADBannerView()
var screenHeight = UIScreen.mainScreen().bounds.height
var adBannerHeight: CGFloat = 50
#IBOutlet var constOne: NSLayoutConstraint!
func appdelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
override func viewWillDisappear(animated: Bool) {
UIiAd.delegate = nil
UIiAd.removeFromSuperview()
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIiAd.alpha = 1
adBannerHeight = 50
UIView.commitAnimations()
constOne.constant = 58
print("ad loaded")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIiAd.alpha = 0
adBannerHeight = 0
UIView.commitAnimations()
constOne.constant = 8
print("error")
}
override func viewWillAppear(animated: Bool) {
UIiAd.delegate = self
UIiAd = self.appdelegate().UIiAd
UIiAd.frame = CGRectMake(0, screenHeight - adBannerHeight , 0, 0)
self.view.addSubview(UIiAd)
}
// End of Ad Controller
Try to change this code:
override func viewWillDisappear(animated: Bool) {
UIiAd.delegate = nil
UIiAd.removeFromSuperview()
}
To:
override func viewWillDisappear(animated: Bool) {
UIiAd.removeFromSuperview()
UIiAd.delegate = nil
}

UIStatusBar won't disappear

I've tried to create a class in Swift, which autohides my UIStatusBar and my navigationController after 1 Second.
My problem is, that the StatusBar is not going to disappear. This is what I got:
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "prefersStatusBarHidden", userInfo: nil, repeats: false)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Fade
}
override func prefersStatusBarHidden() -> Bool {
if (barcounter == 0){
hide()
barcounter = 1
return true
}
else {
show()
barcounter = 0
return false
}
}
#IBAction func picturePressed(sender: AnyObject) {
prefersStatusBarHidden()
}
func hide(){
UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navigationController?.navigationBar.alpha = 0.0
}, completion: nil)
}
func show(){
UIView.animateWithDuration(1, delay: 1, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navigationController?.navigationBar.alpha = 1.0
}, completion: nil)
}
You need to override this method in whichever view controller u want to hide uistatusbar.
override func prefersStatusBarHidden() -> Bool {
return true;
}
if its not work then try this:-
In Info.plist set View controller-based status bar appearance to NO
And call UIApplication.sharedApplication().statusBarHidden = true
hope this helps you.
Alright.. I solved it like that:
I created a new class HeaderAnimationHelper in which I created the useable methods. Like that I can call it from everywhere.
So here you can see the Helper class:
import UIKit
class HeaderAnimationHelper {
static let sharedInstance = HeaderAnimationHelper()
var navi: UINavigationController!
func hideController(var barcounter: Int, navigationController: UINavigationController) -> Int {
navi = navigationController
if (barcounter == 0){
barcounter = 1
UIApplication.sharedApplication().setStatusBarHidden(true, withAnimation: UIStatusBarAnimation.Fade)
hide()
}
else {
show()
barcounter = 0
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
}
return barcounter
}
func hide(){
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navi.navigationBar.alpha = 0.0
}, completion: nil)
}
func show(){
UIView.animateWithDuration(0.5, delay: 0, options: UIViewAnimationOptions.CurveEaseOut, animations: {
self.navi.navigationBar.alpha = 1.0
}, completion: nil)
}
}
and the next class is the main class in which you can put all you code and stuff...
I created it like that:
import UIKit
class ContactMeViewController: UIViewController {
var barcounter = 0
override func viewDidLoad() {
super.viewDidLoad()
NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: "animate", userInfo: nil, repeats: false)
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
}
override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
return UIStatusBarAnimation.Fade
}
#IBAction func picturePressed(sender: AnyObject) {
animate()
}
func animate(){
barcounter = HeaderAnimationHelper.sharedInstance.hideController(barcounter, navigationController: self.navigationController!)
}
}
edit 10/07/15:
I've forgotten to mention, that it's important to add the dependency to the Info.plist
In Info.plist set View controller-based status bar appearance to NO
Watch out this method UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.Fade)
is depricated

How to display iAd after the game scene has loaded?

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 */
}

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