Swift iAd - 超过10个ADBannerView警告实例和CGAffineTransformInvert:奇异矩阵输出 [英] Swift iAd - More than 10 instances of ADBannerView warning and CGAffineTransformInvert: singular matrix output

查看:395
本文介绍了Swift iAd - 超过10个ADBannerView警告实例和CGAffineTransformInvert:奇异矩阵输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想在我的应用程序中设置一个简单的iAd横幅,但我在输出中收到这两个警告:

So I am trying to set up a simple iAd banner in my application but I 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.

<Error>: CGAffineTransformInvert: singular matrix.

这是我用来实现我的 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
}


推荐答案

问题在于每次加载视图时都要创建 ADBannerView的新实例。我们需要做的是在我们的 AppDelegate.swift 中创建一个 ADBannerView ,然后显示 ADBannerView 我们希望拥有iAd横幅。这也称为共享iAd Banner 。在这个例子中,我在我的 AppDelegate.swift 中创建了一个 ADBannerView ,然后将它添加到我的 ViewController.swift 的视图。

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)
    }

不要忘记从 viewWillDisappear中删除代码(动画:Bool )您之前添加的功能。如果您单击横幅然后关闭它,将调用此功能并从我们的视图中删除我们的横幅,并且过早设置我们的横幅委托等于nil会导致问题。

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.

这篇关于Swift iAd - 超过10个ADBannerView警告实例和CGAffineTransformInvert:奇异矩阵输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆