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.
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 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 */
}
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 implemented iAd into my swift sprite kit application, but want to make it so that iAd displays a new ad every minute for example. I supplied my code for how I implemented iAd below.
self.canDisplayBannerAds = true
self.adBannerView?.delegate = self
self.adBannerView?.hidden = true
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
self.adBannerView?.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveWithError error:NSError!) {
}
You can use NSTimer for that. Keep in mind that if your app goes into background mode, it may cause some inaccuracies. However since it's only for showing ads, it's shouldn't be a big issue.
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()
}