iAd Causing constraint conflictions - ios

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 */
}

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 banner goes white on segue

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 */
}

How can I add an iAd banner programmatically to the top centre of the screen?

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.

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