Swift iAd - More than 10 instances of ADBannerView warning and CGAffineTransformInvert: singular matrix output

Solution 1:

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.

Solution 2:

If you don't want to care about the size, position, error handling and the delegate methods of your banner ad you can also use:

self.canDisplayBannerAds = true

This solved the error in my App, because Apple takes also care about the number of instances

I've written a short tutorial about this: link