I've created an iAd banner in the storyboard by dragging into the UITableViewController
In my UITableViewController, I have these codes
#IBOutlet var adBannerView: ADBannerView!
In ViewDidLoad
self.canDisplayBannerAds = true
self.adBannerView.delegate = self
self.adBannerView.hidden = true
And these delegates method
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.adBannerView.hidden = false
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
println("didFailToReceiveAdWithError")
self.adBannerView.hidden = true
}
Sometimes when the UITableViewLoads, it would look like this
http://i.stack.imgur.com/P4p1J.png
The iAd will be at the bottom of the TableView and it moves with it
However sometimes it would just load at the bottom of the View with a fixed position which is what i want.
How do i get the iAd to be fixed at the bottom and doesn't moves with the tableview?
Your issue is that you are actually creating two ADBannerView's here. Once in Interface Builder and another in your viewDidLoad with self.canDisplayBannerAds = true. Remove self.canDisplayBannerAds = true from your viewDidLoad to correct this.
To pin your ADBannerView you created in Interface Builder to the bottom of your applications view you need to set its constraints. Pin your ADBannerView to the bottom of the view with Bottom Space to: Bottom Layout Guide and align it to Align Center X to: Superview in Interface Builder.
This will keep the ADBannerView on the bottom of your applications view and resize it appropriately when on devices with different screen dimensions.
I was unable to add any constraints in the storyboard. I removed the iAd banner from storyboard and just use:
self.canDisplayBannerAds = true
Now it works fine
I had a similar problem so I just coded my manually. Nothing worked when my code was in ViewDidLoad but instead ViewWillAppear all I needed was
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.canDisplayBannerAds = true
self.banner.delegate = self
}
And of course add the ADBannerViewDelegate at the top of the class then its methods:
let banner = ADBannerView(adType: .Banner)
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.view.addSubview(banner)
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
banner.removeFromSuperview()
}
Related
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()
}
I'm trying to figure out what's going on with my constraints... Storyboard thinks there are some conflicting constraints but not that I can see from what I've set manually.
Apparently these are the conflicting constraints:
(
"<_UILayoutSupportConstraint:0x7fc9a0431200 V:[_UILayoutGuide:0x7fc9a0587fc0(0)]>",
"<_UILayoutSupportConstraint:0x7fc9a040e210 V:|-(0)-[_UILayoutGuide:0x7fc9a0587fc0] (Names: '|':UIView:0x7fc9a0584b10 )>",
"<_UILayoutSupportConstraint:0x7fc9a0404300 V:[_UILayoutGuide:0x7fc9a0588ad0(0)]>",
"<_UILayoutSupportConstraint:0x7fc9a0400e00 _UILayoutGuide:0x7fc9a0588ad0.bottom == UIView:0x7fc9a0584b10.bottom>",
"<NSLayoutConstraint:0x7fc9a0584860 V:[UIImageView:0x7fc9a0584f90(0)]>",
"<NSLayoutConstraint:0x7fc9a058afb0 V:[_UILayoutGuide:0x7fc9a0587fc0]-(0)-[UIImageView:0x7fc9a0584f90]>",
"<NSLayoutConstraint:0x7fc9a058b280 V:[UIImageView:0x7fc9a0584f90]-(0)-[_UILayoutGuide:0x7fc9a0588ad0]>",
"<NSLayoutConstraint:0x7fc9a0775890 'UIView-Encapsulated-Layout-Height' V:[UIView:0x7fc9a0584b10(320)]>"
)
Can anyone help me figure out what it thinks is going on? I am using size classes but from what I can see all the correct constraints are installed/uninstalled..
Ok it seems that iAd is causing my constraints to mess up in landscape... hopefully disabling banner ads in landscape will solve this..
You could try to create your iAd banner in code I always prefer it this way.
Here is some working code for you that just worked for me. 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 */
}
I have been wanting to properly fix an iAd to the top of my screen and I tried several solutions but to no avail. I need to fix the issue where you get a white rectangle in place of the iAd banner when disconnected and also it sometimes appears on the top and sometimes on the bottom. How can I make it always appear in the top centre, instead of going from top to bottom and bottom to top randomly?
My code:
class ViewController: UIViewController, ADBannerViewDelegate, UITextFieldDelegate {
#IBOutlet var adBannerView: ADBannerView!
var bannerIsVisible : Bool = false
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = true
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
self.adBannerView?.hidden = true
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.adBannerView?.hidden = false
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
The ADBannerView displaying on the bottom of your devices screen is created by self.canDisplayBannerAds = true. self.canDisplayBannerAds = true can be used for a no hassle way of implementing iAd banners in your application. This will create an ADBannerView for you and show or hide the ADBannerView depending on whether it receives an ad or not from the iAd network.
You need to remove self.canDisplayBannerAds = true from your viewDidLoad.
I have an iAd banner in my application however, sometimes (usually when the iAd has an error) it will shift my other views up. Is it possible to just have the banner view overlap my other views instead of interfere with them?
override func viewDidLoad() {
Banner.hidden = true
Banner.delegate = self
self.canDisplayBannerAds = true
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("Error")
Banner.hidden = true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
Banner.hidden = false
}
Not clear if you are manually adding the iAD banner from a singleton or just using the built in shortcut self.canDisplayBannerAds = true.
If you mix the two together you might have strange results.
Try removing self.canDisplayBannerAds = true and manually add the banner view to your view hierarchy.
this small example might help.
When you opt for the shortcut solution self.canDisplayBannerAds = true, your view hierarchy will be embedded in a bigger view containing the banner and automatically animating as you described. In this case you do not need to conform and implement the delegate methods.
I am having a problem with my code. I am trying to programmatically add an iAd to my view in Xcode. There are a few times when it randomly works, and I run it again without changing any code, and it doesn't work any more. During those few times it randomly works, when I switch to a different view and then back to the one the iAd is in, the iAd is just an empty iAd frame, not showing the animation it should be. It is simply not loading the iAd. Please Help! Code:
import UIKit
import Foundation
import iAd
class ViewController: UIViewController, ADBannerViewDelegate {
//variables*******************************************
var adBannerView = ADBannerView(frame: CGRect.zeroRect)
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.canDisplayBannerAds = true
loadAds()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//iAd**************************************************
func loadAds()
{
adBannerView = ADBannerView(frame: CGRect.zeroRect)
adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2 - 50)
adBannerView.delegate = self
adBannerView.hidden = true
view.addSubview(adBannerView)
}
func bannerViewWillLoadAd(banner: ADBannerView!)
{
println("sort of working1")
}
func bannerViewDidLoadAd(banner: ADBannerView!)
{
adBannerView.hidden = false
println("sort of working2")
}
func bannerViewActionDidFinish(banner: ADBannerView!)
{
println("sort of working3")
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool
{
println("sort of working4")
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!)
{
adBannerView.hidden = true
println("error1")
}
}
UPDATE 1/28/15:
Somehow, the iAds work now without changing the code, but I am experiencing another problem. After I leave the view that the iAd is in, it turns into a blank white frame with the small iAd logo in the bottom corner of the frame. Also, another large problem is that it darkens the tab bar for the view with the iAd.
Not even the println's are working? the iAds are not always displayed, depends of the connectivity to the iAd servers, that's the reason why you should implement the didFailToReceiveAdWithError and hide the banner, in case of no information is comming from the server due to some problem.
If you use the automatic iAd presentation you don't need to do anything else after self.canDisplayBannerAds = true. Your view controller will automatically shrink and auto layout will be triggered when an iAd is available. The iAd will be presented at the bottom.
Thus your sample code can just be:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.canDisplayBannerAds = true
}
If you need a different behaviour here is a simple iAd example. It uses a singleton ADBannerView and can present it at the top of the view.
Anyway consider that the proper way to manually instantiate an ADBannerView is: ADBannerView(adType: ADAdType.Banner)