Shared iAd banner
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
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.
iAds: bannerViewDidLoadAd no longer called after first didFailToReceiveAdWithError
OK, not an ideal solution but here's what I ended up doing.
Whenever I get a didFailToReceiveAdWithError, I wait 10 seconds (to avoid spamming with failures) then recreate the banner.
-(void)replaceAdView {
UIView *adViewSuperview = [adView superview];
[adView removeFromSuperview];
[adView release];
//starting off the screen again
adView = [[NSClassFromString(@"ADBannerView") alloc] initWithFrame:CGRectMake(320, 382, 320, 50)];
adView.delegate = self;
if (adViewSuperview) {
[adViewSuperview addSubview:adView];
}
self.bannerIsInScreenBounds = NO;
}
Related Topics
How to Make a Countdown Timer Like in a Music Player
Customize Google Maps Blue Dot for Current Location
Xcode 8/Swift 3 and Installing Apps on iPhone 4 Running iOS 7
Swiftui - How to Blur the Default Background Color of a View
Passing Arguments to @Selector Method
How to Keep Animated Gifs Animated While Scrolling on iOS Devices
Why Use Corebluetooth Connectperipheral Did Not Call Delegate Methods in iOS8
How to Set The Date and Time Programmatically in iOS
Uiwindow? Does Not Have Member Named Bounds
Passing Data Between View Controllers: from UItableview to a Details View Controller
Where Is Uiview.Init() Documented
Nsdateformatter Still Parsing Instead Having Incorrect Format