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!
Related
I currently have a bug with the app I am working on where when a user taps on an iAd banner and the ad loads, the game resets. By "resets" I mean all their progression (other than the NSUserDefaults data) is reset to 0 and the scene is reset back to the start of the game.
My game primary runs from didMoveToView() and touchesBegan(). In my GameViewController the game scene is built from viewWillLayoutSubviews().
Here is how I control my ads and placement. Not sure if has to do with the placement of my code or the ad.
import UIKit
import SpriteKit
import iAd
// Global Ad Variable
var adBanner: ADBannerView!
class GameViewController: UIViewController, ADBannerViewDelegate {
/* Load Ads */
func loadAds() {
adBanner = ADBannerView(frame: CGRect(x: 0, y: view.bounds.size.height - 50, width: 320, height: 50))
adBanner.delegate = self
adBanner.hidden = true
self.view.addSubview(adBanner)
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
// Create and Configure the scene
scene.size = skView.bounds.size
skView.showsFPS = false // SHOW OR HIDE FRAMES PER SECOND
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)
}
}
override func awakeFromNib() {
super.awakeFromNib()
loadAds()
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return .AllButUpsideDown
} else {
return .All
}
}
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!) {
adBanner.hidden = true
print("There was an error loading ad")
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
print("Ad Loading")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
adBanner.hidden = false
print("Ad Loaded")
}
}
This question has been asked before but I've never found the answer I was looking for.
The story goes like this: I wanted to make a universal game, I had the scene size in the level editor set to the largest size, 2048x1536.
Everything was going fine, until I implemented iAd which started to give me problems with the scene and the way it resized itself to fit on all the devices. When I opened the ad on a device which wasn't 2048 pixels wide the ad stretched the scene and other strange behaviour.
So now I want to make an iPhone version and an iPad version of my game and my question is: which size do I set the scene in the level editor? Do I set it to be the one of the biggest device? or the smallest?
And how do I position the sprites in the scene?
If it can be useful, mine is not a tile based game, I don't have a 'world' or a camera, what's on the scene is what you see in the game.
This is my GameViewController:
class GameViewController: UIViewController, ADBannerViewDelegate {
var adBannerView: ADBannerView!
func loadAds() {
adBannerView = ADBannerView(frame: view.frame)
adBannerView.delegate = self
adBannerView.hidden = true
view.addSubview(adBannerView)
}
override func viewDidLoad() {
super.viewDidLoad()
if let scene = MainMenuScene.unarchiveFromFile("MainMenuScene") as? MainMenuScene {
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = false
skView.showsDrawCount = false
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
loadAds()
}
}
override func shouldAutorotate() -> Bool {
return true
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return [UIInterfaceOrientationMask.LandscapeLeft, UIInterfaceOrientationMask.LandscapeRight]
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
print("Ad about to load")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
adBannerView.hidden = false
print("Displaying the Ad")
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
print("Close the Ad")
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
print("Leave the application to the Ad")
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
adBannerView.hidden = true
print("Ad is not available")
}
}
For the ad I've tried all of the scaleModes and the one who makes the ad work right (not perfectly though) both on iPhone and iPad is .Fill but then my sprites are resized in a way I don't like. And I'm using the Scene Editor so changing the images or the positions of the sprites in code doesn't seem like the most efficient way.
Depending on how you set up object constraints, you could possibly try setting the
SKSceneScaleMode
To
.ResizeFill
Then adding the following variables
var screenSize = UIScreen.mainscreen().bounds
var screenWidth = screenSize.width
var screenHeight = screenSize.height
And when adding your nodes and other objects position and size them based on screenWidth and screenSize
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 {
...
}
I am trying to integrate iAd to a Sprite Kit game.
When I set "canDisplayBannerAds = true" BEFORE adding the game scene, the banner add is shown but the gameScene does not work:
override func viewDidLoad() {
super.viewDidLoad()
canDisplayBannerAds = true
// Configure de main view
if let skView = view as? SKView {
// Create and configure scene
scene = GameScene(size: skView.bounds.size)
scene!.scaleMode = .AspectFill
// Show the scene
skView.presentScene(scene!)
}
}
When I set "canDisplayBannerAds = true" AFTER adding the SKScene, the iAd is shown and the game scene is working now, BUT with a wrong size, bigger than the screen left after the banner is added.
override func viewDidLoad() {
super.viewDidLoad()
// Configure de main view
if let skView = view as? SKView {
skView.showsFPS = true
// Create and configure scene
scene = MarketGameScene(size: skView.bounds.size)
scene!.scaleMode = .AspectFill
scene!.marketGameViewController = self
// Show the scene
skView.presentScene(scene!)
}
canDisplayBannerAds = NSUserDefaults.standardUserDefaults().boolForKey(UserDefaultsKey.ShowAds)
}
What am I missing?
thanks
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. To hide and display the ads use
AdBanner.hidden = true/false
......
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 */
}
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
}