Swift - ADBannerView
I've found a solution, how to implement it. (You can use inside each method "banner.alpha 1.0" or other things, too.)
//import ... your normal imports as UIKit etc.
import iAd
class YourClassViewController: UIViewController, ADBannerViewDelegate {
@IBOutlet var adBannerView: ADBannerView //connect in IB connection inspector with your ADBannerView
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
self.adBannerView.delegate = self
self.adBannerView.hidden = true //hide until ad loaded
}
func bannerViewWillLoadAd(banner: ADBannerView!) {
NSLog("bannerViewWillLoadAd")
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
NSLog("bannerViewDidLoadAd")
self.adBannerView.hidden = false //now show banner as ad is loaded
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
NSLog("bannerViewDidLoadAd")
//optional resume paused game code
}
func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
NSLog("bannerViewActionShouldBegin")
//optional pause game code
return true
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
NSLog("bannerView")
}
//... your class implementation code
}
See the following answer, on how to do it without IBBuilder!
iOS/Swift: more than 10 instances of ADBannerView
Why are your making an outlet for bannerView. After iOS 7 things have been changed. If you want iAds then all you need to do is add the iAdsFramework
, import the framework in your UIViewController
and in your viewDidLoad()
write the following code
self.canDisplayBannerAds = true;
It will start displaying ads.
UPDATE
Forget the canDisplayBannerAds
thing as your requirement is different. Just create a single instance for banner View in your AppDelegate
and in your view controller get that instance , set frames and add it as subview and don't forget to remove it when your controller disappears.check this link you will get the idea. stackoverflow.com/questions/28514758/…
How to add an adbannerview in UITableViewController in Swift?
Just drag a iAd Banner View to the footer of your UITableViewController in the storyboard, setup the @IBOutlet and setup the layout constraints.
Swift iAd - More than 10 instances of ADBannerView warning and CGAffineTransformInvert: singular matrix output
The issue is every time you load your view you are creating a new instance of ADBannerView
. What we need to do is create a ADBannerView
once in our AppDelegate.swift
and then present this ADBannerView
on which ever views we would like to have an iAd banner. This is also called a Shared iAd Banner. In this example, I've created an ADBannerView
in my AppDelegate.swift
and then added it to my ViewController.swift
's view.
AppDelegate.swift
import UIKit
import iAd // Import iAd
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate { // Include the delegate for our banner
var window: UIWindow?
var adBannerView = ADBannerView() // Create our one ADBannerView
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Set delegate and hide banner initially
adBannerView.delegate = self
adBannerView.hidden = true
return true
}
func bannerViewDidLoadAd(banner: ADBannerView!) {
print("bannerViewDidLoadAd")
adBannerView.hidden = false
}
func bannerViewActionDidFinish(banner: ADBannerView!) {
print("bannerViewActionDidFinish")
}
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
print("didFailToReceiveAdWithError: \(error)")
adBannerView.hidden = true
}
ViewController.swift
import UIKit
class ViewController: UIViewController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate // Create reference to our app delegate
override func viewWillAppear(animated: Bool) {
// Position
appDelegate.adBannerView.center = CGPoint(x: view.frame.midX,
y: view.frame.height - appDelegate.adBannerView.frame.height / 2)
// Add to view
view.addSubview(appDelegate.adBannerView)
}
Don't forget to remove the code from your viewWillDisappear(animated: Bool)
function that you added previously. If you click on the banner and then dismiss it this function will be called and removing our banner from our view and setting our banners delegate equal to nil too soon will cause issues.
AdBannerView in TableViewController is shown after TableView
If all you want is a banner ad at the bottom of the screen, you can simply say something like
override func viewDidLoad() {
super.viewDidLoad()
self.canDisplayBannerAds = true
}
And it will work automagically as they say.
I would recommend watching this video to understand the iAd API
Swift / iAd bannerView / how to set the position
Try as Follow:
var adBannerView: ADBannerView
adBannerView = ADBannerView(frame: CGRectMake(0, 0, 50, screenBounds.width))
adBannerView.center = CGPoint(x: screenBounds.width/2, y: screenBounds.height-adBannerView.frame.height)
adBannerView.delegate = self
adBannerView.hidden = true
view.addSubview(adBannerView)
Hiding iAd ADBannerView in Swift when ad fails to load - no delegate or delegate does not implement didFailToReceiveAdWithError
Delegate methods will not be called when using self.canDisplayBannerAds = true
. You need to create an ADBannerView
yourself and set its delegate
for the delegate methods to be called, for example, bannerView.delegate = self
.
self.canDisplayBannerAds = true
is a no hassle way of implementing iAd banners. It will create an ADBannerView
for you, display it if it receives an ad, and hide it if it does not receive an ad. There is no need to implement delegate methods when implementing your iAd banner in this way.
So you have two options, remove var bannerView = ADBannerView()
and use the iAd banner that self.canDisplayBannerAds = true
provides, or remove self.canDisplayBannerAds = true
and finish implementing your own ADBannerView
.
If you decide to implement your own ADBannerView
check my answer here, just ignore the AdMob implementation.
ADBannerView not showing up
You have not added the banner view to your view. You have called self.view.addSubview(iAdBanner)
in bannerViewDidLoadAd
which will not get called before adding the banner view. You need to add the banner view beforehand (e.g. in viewDidLoad
).
Also, you cant change the banner view frame to any size you want .
Read through the Banner View Sizes in iAD Programming Guide (actually read the entire guide). It has code snippets (in ObjC not Swift though).
It clearly says :
iAd supports different banner sizes for portrait and landscape apps.
The exact size of advertisements depends on the device the banner is
being shown on. On an iPhone, a portrait advertisement is 320 x 50
points and 480 x 32 points for a landscape advertisement. On an iPad,
a portrait advertisement is 768 x 66 points and 1024 x 66 points for a
landscape advertisement. In the future, additional sizes may be
exposed by iAd.
And in the next paragraph:
To ensure that advertisements are displayed properly, a banner view
must always be sized to match one of the built-in advertising sizes.
The ADBannerView class enforces this by preventing you from changing
the frame directly. Instead, you change a banner view’s frame by
setting the currentContentSizeIdentifier property. Changing the value
stored in this property resizes the banner view’s frame to the match
the size for the provided identifier.
How to make close button for iAdbannerView
This is the way to do it programmatically :
Create a UIButton
under adBannerView
:var button = UIButton()
In viewDidLoad
instantiate the button and place it at the right top side of the adBannerView
:
button.frame = CGRectMake(self.view.center.x + 150, self.view.frame.size.height - 66, 10, 10)
button.setBackgroundImage(UIImage(named: "Yourclosebuttonimagename"), forState: UIControlState.Normal)
self.view.addSubview(button)
button.addTarget(self, action: "hideAd:", forControlEvents: UIControlEvents.TouchUpInside)
The function will be called hideAd
:
func hideAd(sender:AnyObject){
self.adBannerView.hidden = true
self.button.hidden = true
// or do
self.button.removeFromSuperview()
self.adBannerView.removeFromSuperview()
}
I recommend you remove self.canDisplayBannerAds = true
because self.adBannerView!.delegate = self
is enough to run the iAd.
Good luck !
Related Topics
How to Get Title from Wkinterfacebutton
Get a List of Nodes in an Specific Area
Is Is Possible to Use Vapor 3 Postgres Fluent in a Standalone Script
Limit Textfield to X Amount of Characters Using Swiftui
Get Compiler Error in Swift Indexof()
Skaction.Movebyx Not Setting Physicsbody.Velocity
Implementing Stringliteralconvertible on Nsurl
Navigationview Swiftui Shows Split View in iPad
Why Is There a "Plus" Icon at the Top Right Corner of My View
Swift: Oslog/Os_Log Not Showing Up in Console App
Which Value Types in Swift Supports Copy-On-Write
Create Complicated Nscompoundpredicate in Swift 3
Tapping an Mkmapview in Swiftui
How to Segue Values When My Viewcontroller Is Embedded in an Uinavigationcontroller
Why Upload Alamofire Background Request Don't Executes in Background
Crop/Mask Circular Image Node in Sprite Kit Gives Jagged Edges