Reading through Apple's documentation on iAD I got the impression, that if I'll add the ADBannerView to my controller's view tree - other views will be scaled to fit the add. But apparently that's not the case as you can see here :
That's the code I'm using :
import UIKit
import SpriteKit
import iAd
class GameViewController: UIViewController, ADBannerViewDelegate {
var mainView: SKView!
let adBanner = ADBannerView(adType: .Banner)
override func viewDidLoad() {
super.viewDidLoad()
mainView = self.view as? SKView
mainView.showsDrawCount = true
mainView.showsNodeCount = true
mainView.showsFPS = true
adBanner.delegate = self
adBanner.center = CGPoint(x: adBanner.center.x, y: mainView.bounds.size.height - adBanner.frame.size.height / 2)
adBanner.hidden = true
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("error: \(error)")
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
print("WILL LOAD BANNER")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
print("Loaded Ad \(banner)")
adBanner.hidden = false
}
override func viewWillAppear(animated: Bool) {
let helloScene = HelloScene(size: mainView.frame.size)
mainView.presentScene(helloScene)
mainView.addSubview(adBanner)
}
override func shouldAutorotate() -> Bool {
return false
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
return UIInterfaceOrientationMask.AllButUpsideDown
} else {
return UIInterfaceOrientationMask.All
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func prefersStatusBarHidden() -> Bool {
return true
}
}
Am I missing something ? If I'll set controller's self.canDisplayBannerAds = true then banner is displayed properly at the bottom with the rest scaled as I would expect.
Is it because I'm using presentScene, which adds SKScene and not a view ?
UPDATE
I've tried adding constraints, but I guess there's a lot of reading ahead of me cause I couldn't yet figure that out :) I googled for a while and came up with this, but that didn't change anything so probably I'm doing it wrong (added that to viewDidLoad :
mainView.addSubview(adBanner)
let viewsDictionary = ["adBanner": adBanner]
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[adBanner]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: viewsDictionary))
view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[adBanner]|", options: NSLayoutFormatOptions.AlignAllBaseline, metrics: nil, views: viewsDictionary))
You can replace your complete code with
self.canDisplayBannerAds = true
Here is a short tutorial.
Related
My app was approved just a couple of days ago and had an iAd banner on the bottom of the screen. I know that there it can take up to a week for ads to start appearing, but while I'm waiting.. can someone make sure that I don't have any errors in my code?
import UIKit
import SpriteKit
import iAd
import GameKit
class GameViewController: UIViewController, ADBannerViewDelegate, GKLocalPlayerListener {
var adBanner: ADBannerView!
override func viewDidLoad() {
super.viewDidLoad()
authenticateLocalPlayer()
if let scene = GameScene(fileNamed:"GameScene") {
// Configure the view.
let skView = self.view as! SKView
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
}
func loadBanner() {
adBanner = ADBannerView(frame: CGRect.zero)
adBanner.center = CGPoint(x: adBanner.center.x, y: view.bounds.size.height - adBanner.frame.size.height / 2)
adBanner.delegate = self
adBanner.hidden = true
view.addSubview(adBanner)
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
adBanner.hidden = false
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
adBanner.hidden = true
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
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 authenticateLocalPlayer() {
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
}
else {
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
}
Right now it just displays a white rectangle. Is there anything wrong with my code, or do I just need to keep waiting for my app to start receiving ads?
iAd is officially discontinued as of tomorrow (June 30th, 2016). I am unsure of the "sign up" process now, but I believe Apple stopped registering apps to receive ads from their network some months ago
Who calls the loadBanner()?
You might consider overriding viewDidAppear() and shifting the code there.
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")
}
}
I am making the iAds in my app to be loaded in the app delegate. It loads fine however in the view controllers, it will not show.
My code in the app delegate to declare the ad is
var UIiAd: ADBannerView = ADBannerView()
My code in the view controller is
class HelpViewController: UIViewController, ADBannerViewDelegate {
//MARK: - Properties
var UIiAd: ADBannerView = ADBannerView()
//MARK: - did something
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func viewWillAppear(animated: Bool) {
//super.viewWillAppear(animated)
let ScreenHeight = UIScreen.mainScreen().bounds.height
UIiAd.delegate = self
UIiAd = self.appDelegate().UIiAd
UIiAd.frame = CGRectMake(0, ScreenHeight - 50, 0, 0)
UIiAd.hidden = true
self.view.addSubview(UIiAd)
}
override func viewWillDisappear(animated: Bool) {
UIiAd.delegate = nil
UIiAd.removeFromSuperview()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: - iAd
func bannerViewDidLoadAd(banner: ADBannerView!) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(1)
UIiAd.hidden = false
UIView.commitAnimations()
print("Did load ad")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
UIView.beginAnimations(nil, context: nil)
UIView.setAnimationDuration(0)
UIiAd.hidden = true
UIView.commitAnimations()
print("Did fail to receive ad with error \(error)")
}
//MARK: - Functions
func appDelegate() -> AppDelegate {
return UIApplication.sharedApplication().delegate as! AppDelegate
}
}
What seems to be the problem is, bannerViewDidLoadAd doesn't ever get called. How would I unhide the banner if it loads?
I have actually fixed this. What you need to do in the app delegate is
class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate
Then you want to make a bool that changes if the ad is loaded
Then you want to add the function func bannerViewDidLoadAd(banner: ADBannerView!) {
adLoaded = true
}
This will change the value when the ad is loaded
Then in your view controller you want to do
override func viewWillAppear(animated: Bool) {
//super.viewWillAppear(animated)
let ScreenHeight = UIScreen.mainScreen().bounds.height
UIiAd.delegate = self
UIiAd = self.appDelegate().UIiAd
UIiAd.frame = CGRectMake(0, ScreenHeight - 50, 0, 0)
canDisplayBannerAds = true
if appDelegate().adLoaded == true {
self.view.addSubview(UIiAd)
}
}
This will add the ad to your view controller will not if the ad is not loaded
Hey This is the code I used for Xcode 6.4 but when I go to Xcode 7 there's an error. So at first I just deleted the ConstantsH and V. That got rid of the errors but then I look at the app and the banner is no longer at the bottom of the scree now its at the top. How do I change that back in Xcode 7? Thanks in advance.
Code:
class GameViewController: UIViewController, ADBannerViewDelegate{
var bannerAd = ADBannerView(adType: ADAdType.Banner)
override func viewDidLoad() {
super.viewDidLoad()
bannerAd.delegate = self
self.view.addSubview(bannerAd)
let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("|[bannerAd]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerAd(50)]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
self.view.addConstraints(constraintsH)
self.view.addConstraints(constraintsV)
Full File Below:-----------------
import StoreKit
import SpriteKit
import GameKit
import iAd
class GameViewController: UIViewController, ADBannerViewDelegate{
var bannerAd = ADBannerView(adType: ADAdType.Banner)
override func viewDidLoad() {
super.viewDidLoad()
bannerAd.delegate = self
self.view.addSubview(bannerAd)
let constraintsH = NSLayoutConstraint.constraintsWithVisualFormat("|[bannerAd]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
let constraintsV = NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerAd(50)]|", options: nil, metrics: nil, views: ["bannerAd":bannerAd])
self.view.addConstraints(constraintsH)
self.view.addConstraints(constraintsV)
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
let localPlayer = GKLocalPlayer()
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if (viewController != nil) {
let vc: UIViewController = self.view!.window!.rootViewController!
vc.presentViewController(viewController!, animated: true, completion: nil)
}
else {
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
let scene = GameScene()
let sKView = self.view! as! SKView
sKView.ignoresSiblingOrder = true
scene.size = sKView.bounds.size
scene.scaleMode = .AspectFill
let reveal = SKTransition.fadeWithDuration(0.45)
sKView.presentScene(scene, transition: reveal)
}
override func shouldAutorotate() -> Bool {
if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||
UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {
return false;
}
else {
return true;
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Release any cached data, images, etc that aren't in use.
}
override func prefersStatusBarHidden() -> Bool {
return true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
NSLog("bannerViewWillLoadAd")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
NSLog("bannerViewDidLoadAd")
self.bannerAd.hidden = false//now show banner as ad is loaded
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
NSLog("bannerViewDidLoadAd")
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
NSLog("bannerViewActionShouldBegin")
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("bannerView")
self.bannerAd.hidden = true
}
}
This is how you do it
bannerAd.center = CGPoint(x: bannerAd.center.x, y: view.bounds.size.height - bannerAd.frame.size.height * 0.05)
I have almost completed a game for IOS in Swift and am having an issue with my banner. I managed to find some code to stop it resizing the view frame, but now it seems to resize completely randomly; sometimes not resizing (as desired) sometimes resizing as is default.
extension SKNode {
class func unarchiveFromFile(file : String) -> SKNode? {
if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
var sceneData = NSData(contentsOfFile: path, options: .DataReadingMappedIfSafe, error: nil)!
var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)
archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as! GameScene
archiver.finishDecoding()
return scene
} else {
return nil
}
}
}
class GameViewController: UIViewController, ADBannerViewDelegate {
let gameCenterPlayer = GKLocalPlayer.localPlayer()
var bannerView: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 = 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
NSNotificationCenter.defaultCenter().addObserver(self, selector: "updateNoAds:", name: "noAdsID", object: nil)
skView.presentScene(scene)
}
// disabling ads if no-ads is purchased
if bannerAdsOff != true {
self.canDisplayBannerAds = true
} else {
self.canDisplayBannerAds = false
}
bannerView = ADBannerView()
bannerView?.delegate = self
bannerView?.frame = CGRectZero
bannerView?.backgroundColor = UIColor.clearColor()
bannerView!.frame = CGRectMake(0, self.view.frame.size.height - self.bannerView!.frame.size.height, self.bannerView!.frame.size.width, self.bannerView!.frame.size.height)
bannerView?.hidden = true
view.addSubview(bannerView!)
}
func updateNoAds(notification: NSNotification) {
self.canDisplayBannerAds = false
println("turning can Display banner ads off")
self.bannerView?.hidden = true
println("bannerview hidden = \(self.bannerView?.hidden)")
}
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 bannerViewDidLoadAd(banner: ADBannerView!) {
if bannerAdsOff != true {
self.bannerView?.hidden = false
}
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return willLeave
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.bannerView?.hidden = true
}
}
I have read somewhere that it has to do with the canDisplayBannerAds setting, but if I change that to false, it just means no ads are shown at all.
I'm hoping to release tomorrow and would really like to have this fixed for the release :)
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 spritkit game it did not resize the scene.
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 */
}