Swift iAd – More than 10 instances of adBannerView warning and CGAffineTransformInvert: singular matrix output

| | August 11, 2015

So I am trying to set up a simple iAd banner on my app but am getting these two warnings in the output:

WARNING: More than 10 instances of ADBannerView or ADInterstitialView 
currently exist. This is a misuse of the iAd API, and ad performance will 
suffer as a result. This message is printed only once.

and

<Error>: CGAffineTransformInvert: singular matrix.

This is what I am using to implement my adBannerView:

var adBannerView = ADBannerView()

func loadAds() {
    adBannerView = ADBannerView(frame: CGRect.zeroRect)
    adBannerView.center = CGPoint(x: adBannerView.center.x, y: view.bounds.size.height - adBannerView.frame.size.height / 2)
    adBannerView.delegate = self
    adBannerView.hidden = true
    view.addSubview(adBannerView)
}

//BannerView did load ad
func bannerViewDidLoadAd(banner: ADBannerView!) {
    adBannerView.hidden = false
}
//BannerView failed to load
func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    adBannerView.hidden = true
}

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    loadAds()
    //(rest of the code is from here onwards)

I tried adding this to stop the first error: (hasn’t worked)

//BannerView will disappear
override func viewWillDisappear(animated: Bool) {
    adBannerView.removeFromSuperview()
    adBannerView.delegate = nil
}

Any help is appreciated, thank you :)

2 Responses to “Swift iAd – More than 10 instances of adBannerView warning and CGAffineTransformInvert: singular matrix output”

  1. 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

  2. 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 app delegate and then added it to my view controller’s view.

    // AppDelegate.swift
    import UIKit
    import iAd // Import iAd
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
    var window: UIWindow?
    var adBannerView = ADBannerView() // Create our one ADBannerView
    

    // ViewController.swift    
    import UIKit
    import iAd // Import iAd
    
    class ViewController: UIViewController, ADBannerViewDelegate { // Include the delegate for our banner
    
    let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate // Create reference to the app delegate
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadAds()
    }
    
    func loadAds() {
        self.appDelegate.adBannerView.removeFromSuperview()
        self.appDelegate.adBannerView.delegate = nil
        self.appDelegate.adBannerView = ADBannerView(frame: CGRect.zeroRect)
        self.appDelegate.adBannerView.center = CGPoint(x: view.bounds.size.width / 2, y: view.bounds.size.height - self.appDelegate.adBannerView.frame.size.height / 2)
        self.appDelegate.adBannerView.delegate = self
        self.appDelegate.adBannerView.hidden = true
        view.addSubview(self.appDelegate.adBannerView)
    }
    
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        println("bannerViewDidLoadAd")
        self.appDelegate.adBannerView.hidden = false
    }
    
    func bannerViewActionDidFinish(banner: ADBannerView!) {
        println("bannerViewActionDidFinish")
    }
    
    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        println("didFailToReceiveAdWithError")
        self.appDelegate.adBannerView.hidden = true
    }
    

    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.

Leave a Reply