简体   繁体   English

Swift iAd-超过10个ADBannerView警告和CGAffineTransformInvert实例:奇异矩阵输出

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

So I am trying to set up a simple iAd banner in my application but I am getting these two warnings in the output: 因此,我尝试在应用程序中设置一个简单的iAd标语,但在输出中收到以下两个警告:

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

The issue is every time you load your view you are creating a new instance of ADBannerView . 问题是,每次加载视图时,您都在创建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. 我们需要做的是在AppDelegate.swift创建一个ADBannerView ,然后展示该ADBannerView ,我们希望在其上面的任何视图上都拥有iAd标语。 This is also called a Shared iAd Banner . 这也称为“ 共享iAd标语” In this example, I've created an ADBannerView in my AppDelegate.swift and then added it to my ViewController.swift 's view. 在此示例中,我在AppDelegate.swift创建了一个ADBannerView ,然后将其添加到ViewController.swift的视图中。

AppDelegate.swift 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 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. 不要忘记从以前添加的viewWillDisappear(animated: Bool)函数中删除代码。 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. 如果您单击横幅,然后将其关闭,则将调用此函数,并且从视图中删除横幅并将横幅委托设置为nil太早会导致问题。

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 这解决了我的App中的错误,因为Apple还考虑了实例数

I've written a short tutorial about this: link 我已经写了一个简短的教程: link

If you want the banner to persist between the tabs and not disappear when rapidly switching tabs, you've got to do the iAd Suite approach: http://developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html (Check out BannerViewController.m file - not in Swift, but it's not hard to convert it) 如果您希望横幅广告在标签之间保持不变,并且在快速切换标签时不消失,则必须采用iAd Suite方法: http//developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/ Intro.html出BannerViewController.m文件-不在Swift中,但转换起来并不难)

This approach also doesn't require you to modify any go the view controllers within your tabs. 这种方法也不需要您修改标签内的所有go控制器。 You simply have to have a relationship segue from your tab bar controller to a View Controller that has container view embedded into it. 您只需要将选项卡栏控制器与嵌入了容器视图的View Controller建立关系即可。 Do that in your storyboard. 在情节提要中执行此操作。 Also, you need to set Custom Class to BannerViewController for each tab, and embed your content into the embedded view. 另外,您需要为每个选项卡将Custom Class设置为BannerViewController,并将您的内容嵌入到嵌入式视图中。 Take a look at this post for details on how to do it it the Storyboard: https://stackoverflow.com/a/16205420/5007500 看看这篇文章以了解如何执行故事板的详细信息: https : //stackoverflow.com/a/16205420/5007500

If you are not using Storyboard - you need to set BannerViewController as parent view controller for each of your tabs. 如果您不使用Storyboard,则需要将BannerViewController设置为每个选项卡的父视图控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM