iAd banner goes white on segue - ios

I am using Xcode 7, Swift 2.0. This problem happens both in the simulator, and in my actual app which is available on the App Store. Many times (not always), when I perform a segue in my app the adBanner goes plain white for a bit before loading a new ad. I'm confused because an ad is available, even when it's white!
Here is my code:
I initialize the ADBannerView:
var adBanner = ADBannerView(adType: ADAdType.Banner)
In my viewDidLoad:
self.canDisplayBannerAds = true
self.adBanner.delegate = self
self.adBanner.hidden = true
self.adBanner.alpha = 0
self.adBanner.frame.origin.y = self.view.frame.height-self.adBanner.frame.height
self.view.addSubview(self.adBanner)
My viewDidDisappear:
override func viewDidDisappear(animated: Bool) {
super.viewDidDisappear(true)
adBanner.removeFromSuperview()
if( deviceType.isEqualToString("iPhone") )
{
adBanner.delegate = nil
}
}
In my class:
func bannerViewDidLoadAd(banner: ADBannerView!)
{
self.adBanner.hidden = false
UIView.animateWithDuration(0.5, animations: {self.adBanner.alpha = 1})
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!)
{
self.adBanner.hidden = true
UIView.animateWithDuration(0.5, animations: {self.adBanner.alpha = 0})
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool
{
return willLeave
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
When running the app with Xcode, I occasionally get this message despite the fact that I have implemented the delegate method:
ADBannerView: Unhandled error (no delegate or delegate does not implement didFailToReceiveAdWithError:): Error Domain=ADErrorDomain Code=5 "The operation couldn’t be completed. Banner view is visible but does not have content" UserInfo=0x9632d30 {ADInternalErrorCode=5, NSLocalizedFailureReason=Banner view is visible but does not have content}
Edit: The problem is using canDisplayBannerAds results in delegate methods not being called. More info here: Hiding iAd ADBannerView in Swift when ad fails to load - no delegate or delegate does not implement didFailToReceiveAdWithError

Here is some working code for you that just worked for me. This does not 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. In my spritekit game it did not have any problems with becoming white when transitioning.
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

How to add iAd in SpriteKit and deactivate it when a scene changes?

In my game file GameScene, I wrote code for iAd to be displayed there.
func addiAd(){
bannerView = ADBannerView(adType: .Banner)
bannerView.delegate = self
bannerView.hidden = true
bannerView.frame = CGRectOffset(bannerView.frame, 0.0, 0.0)
bannerView.center = CGPointMake(bannerView.center.x,
(view?.bounds.size.height)! - bannerView.frame.size.height/2)
view!.addSubview(bannerView)
print("iAd is working")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
bannerView.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
bannerView.removeFromSuperview()
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
bannerView.hidden = true
}
Well, it first looks like it works, but not perfectly because when a scene has transitioned to another game scene file called GameOverScene, my bannerView does not disappear and stays where it appeared forever until the run is over. I want to deactivate this. Is my code wrong written above? In my source code, in total, I want two adBanner views in GameScene and GameOverScene respectively. My assumption for the error is that I did not write those code in GameViewController, but I am unsure about it. Could you show me how to implement it and explain me where it has to be written?
In the iAd delegate methods you could change the alpha properties of your banner. For example:
func bannerViewDidLoadAd(banner: ADBannerView!) {
bannerView.alpha = 1.0
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
bannerView.alpha = 0.0
}
And then when you're changing scenes or want to make sure that the iAd banner does not appear you could set its hidden property. For example:
func hideBanner() {
// Call whenever you don't want the ADBannerView to be on screen
bannerView.hidden = true
}
func showBanner() {
// Call when you want the ADBannerView to be on screen
bannerView.hidden = false
}
This would require you to update your addiAd function also: Change bannerView.hidden = true to bannerView.alpha = 0.0.
And finally you shouldn't be removing the iAd banner from the Superview here:
func bannerViewActionDidFinish(banner: ADBannerView!) {
bannerView.removeFromSuperview()
}

iAd unavailable in GameScene.swift?

I have imported iAd into my GameScene.swift file and for some reason I cannot call the self.canDisplayBannerAds() method. Does anyone know how to solve this?
Here is some working code for you that just worked for me. This does not 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 */
}
If you want to use canDisplayBannerAds implement it like so:
import UIKit
import iAd // Import iAd
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
showBanner()
}
func showBanner() {
// Creates a self managed ADBannerView
self.canDisplayBannerAds = true
}
func hideBanner() {
// Removes the ADBannerView
self.canDisplayBannerAds = false
}
Using canDisplayBannerAds creates and manages an ADBannerView for you. There's no need to implement additional methods and the ADBannerView will hide/show itself depending on if there is an advertisement available or not.
Did you remember to inherrit from ADBannerViewDelegate AND import iAd
yourVC: UIViewController, ADBannerViewDelegate {
...
}

iAd didFailToReceiveAdWithError not working sometimes, displays white box

I'm using Swift and SpriteKit, and everything is made inside my GameViewController and GameScene.
This is all code concerning the ads inside my GameViewController:
class GameViewController: UIViewController, ADBannerViewDelegate, GADBannerViewDelegate {
var adBannerView: ADBannerView!
var gadBannerView: GADBannerView!
var bannerDisplayed = false
var bannerNow = "iAd"
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? 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 = .AspectFill
if((UIDevice.currentDevice().userInterfaceIdiom == .Phone) && (UIScreen.mainScreen().bounds.height <= 480)) {
println("<= iPhone 4S")
scene.size.height = skView.bounds.size.height * 2
scene.size.width = skView.bounds.size.width * 2
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "hideADBanner", name: "hideAd", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "showADBanner", name: "showAd", object: nil)
self.adBannerView = self.loadAds()
skView.presentScene(scene)
println(gadBannerView)
}
}
func loadAds()->ADBannerView{
var adBannerView2 = ADBannerView(frame: CGRect.zeroRect)
adBannerView2.center = CGPoint(x: adBannerView2.center.x, y: view!.bounds.size.height - adBannerView2.frame.size.height / 2)
adBannerView2.delegate = self
adBannerView2.hidden = true
bannerNow = "iAd"
self.view?.addSubview(adBannerView2)
return adBannerView2
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
println("left for ad")
return true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("ad did load.")
println("Delegate: \(adBannerView.delegate)")
adBannerView.hidden = false
//gadBannerView?.removeFromSuperview()
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
//adBannerView.removeFromSuperview()
//self.gadBannerView = self.createGADBanner()
}
func createGADBanner()->GADBannerView {
var ad = GADBannerView()
ad = GADBannerView(frame: CGRectMake(0, 0, self.view.frame.size.width, 50))
ad.delegate = self
ad.rootViewController = self
ad.adUnitID = "xxxxxxxxxxx"
bannerNow = "GAD"
var reqAd = GADRequest()
//reqAd.testDevices = [GAD_SIMULATOR_ID] // If you want test ad's
ad.loadRequest(reqAd)
self.view.addSubview(ad)
println(ad)
return ad
}
func adViewDidReceiveAd(view: GADBannerView!) {
println("adViewDidReceiveAd:\(view)");
bannerDisplayed = true
relayoutViews()
}
func adView(bannerView: GADBannerView!, didFailToReceiveAdWithError error: GADRequestError!) {
gadBannerView.removeFromSuperview()
}
func relayoutViews() {
if (bannerDisplayed) {
var bannerFrame = gadBannerView!.frame
bannerFrame.origin.x = 0
bannerFrame.origin.y = self.view.bounds.size.height - bannerFrame.size.height
gadBannerView!.frame = bannerFrame
}
}
func hideADBanner() {
println(self.gadBannerView)
println(bannerNow)
//adBannerView?.hidden = true
//gadBannerView?.hidden = true
adBannerView?.removeFromSuperview()
gadBannerView?.removeFromSuperview()
}
func showADBanner() {
self.adBannerView = self.loadAds()
//adBannerView?.hidden = false
//gadBannerView?.hidden = false
}
So my plan is to get ads from AdMob when iAd is not working, but the problem is that my iAd "always works", even though I've changed the fill-rate inside the Developer tools in my iPhone.
When iAd loads the ad it works fine, the function bannerViewDidLoadAd() is called and the ad is displayed. Although sometimes when I load iAd it says that I've entered bannerViewDidLoadAd() but it only displays a white "box" with the iAd logo in the bottom. Now, this is not what I want because then AdMob is never gonna show. The println always put out the same delegate so it seems like that works at least.
This view never refreshes because everything, as I said, is handled in the GameScene where it's just removing and adding nodes.
Thank you!
You're over complicating things immensely with bool's, unnecessary functions, and it looks like you've actually created two ADBannerViews once in Interface Builder, self.adBannerView, and once programmatically, var adBannerView: ADBannerView!.
I've created an example with comments to help you along the way. Whats happening here is we are creating an ADBannerView, setting it up once in our viewDidLoad, and then hiding/showing our ADBannerView depending on if it receives an ad from the iAd network or not. I've left out the AdMob implementation as it should be pretty straight forward now.
import UIKit
import iAd // Import iAd
class ViewController: UIViewController, ADBannerViewDelegate { // Include the delegate for our ADBannerView
var adBannerView = ADBannerView() // Create our ADBannerView
// Create your GADBannerView here
override func viewDidLoad() {
super.viewDidLoad()
loadAds()
}
func loadAds() {
// Setup our ADBannerView
adBannerView = ADBannerView(frame: CGRect.zeroRect)
adBannerView.center = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
adBannerView.delegate = self
adBannerView.hidden = true
view.addSubview(adBannerView)
// Setup your GADBannerView here in the same manner
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
println("bannerViewDidLoadAd")
// We received an ad from iAd. Lets show our banner
adBannerView.hidden = false
// Hide your GADBannerView here
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
println("bannerViewActionDidFinish")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("didFailToReceiveAdWithError: \(error)")
// We failed to receive an ad from iAd. Lets hide our banner and print the error
adBannerView.hidden = true
// Show your GADBannerView here
}

Hide adBanner after view controller change

So I have this code.
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
adBanner.delegate = self
adBanner.hidden = true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("adBanner Error")
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
adBanner.hidden = false
}
It works fine but when I transition to one of my other view controllers, then go back, the adBanner is visible but empty for x amount of time (say maybe 15 seconds) before loading a new ad. How can I tell the adBanner to become hidden again until the new ad is loaded?

iAd not showing Any ads, Swift, ViewController

when I run the game a white box appears at the top of the game which I'm guessing the ads are suppose to appear in there, but no ads ever appear!
Am I doing something wrong, missing a certain piece of code somewhere, or blind? haha.
I'm quite new to this iAd and everything!
Here is an image of it
Here is the ViewController Code!
import UIKit
import SpriteKit
import iAd
class GameViewController: UIViewController, ADBannerViewDelegate{
#IBOutlet var Banner: ADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
// Configure the view.
let skView = self.view as SKView
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
loadAds()
}
}
func loadAds() {
self.canDisplayBannerAds = true
Banner = ADBannerView(frame: CGRect.zeroRect)
Banner.center = CGPointMake(Banner.center.x, view.bounds.size.height - Banner.frame.size.height / 2)
Banner.delegate = self
Banner.hidden = true
view.addSubview(Banner)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.Banner.hidden = true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.Banner.hidden = false
}
Your issue is that you are using self.canDisplayBannerAds = true in addition to creating an ADBannerView programmatically. Remove self.canDisplayBannerAds = true from your loadAds function.
It seems to be working all of the sudden! Was getting error message, but it has gone away and the Ad has actually loaded! :)
Don't know what was going on!

Resources