所以我试图在我的应用程序中设置一个简单的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.
和
<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)
我尝试添加此项以停止第一个错误:(尚未奏效)
//BannerView will disappear
override func viewWillDisappear(animated: Bool) {
adBannerView.removeFromSuperview()
adBannerView.delegate = nil
}
答案 0 :(得分:18)
问题是,每次加载视图时,您都在创建ADBannerView
的新实例。我们需要做的是在我们的ADBannerView
中创建AppDelegate.swift
一次,然后展示我想要拥有iAd横幅的ADBannerView
视图。这也称为Shared iAd Banner。在此示例中,我在ADBannerView
中创建了AppDelegate.swift
,然后将其添加到我的ViewController.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 强>
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(animated: Bool)
功能中删除代码。如果您单击横幅然后将其关闭,则会调用此函数并从我们的视图中删除我们的横幅,并且过早将横幅委托设置为nil会导致问题。
答案 1 :(得分:1)
如果您不想关注横幅广告的尺寸,位置,错误处理和委托方法,您还可以使用:
self.canDisplayBannerAds = true
这解决了我的应用程序中的错误,因为Apple也关心实例的数量
我写了一篇关于此的简短教程: link
答案 2 :(得分:1)
如果您希望横幅在标签之间保持不变并且在快速切换标签时不会消失,则必须执行iAd Suite方法:http://developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html(查看BannerViewController.m文件 - 不在Swift中,但是转换它并不难)
此方法也不要求您修改选项卡中的任何视图控制器。您只需要从标签栏控制器到嵌入了容器视图的View Controller建立关系segue。在你的故事板中做到这一点。此外,您需要为每个选项卡将Custom Class设置为BannerViewController,并将您的内容嵌入到嵌入视图中。请看一下这篇文章,详细了解如何在故事板上进行操作:https://stackoverflow.com/a/16205420/5007500
如果您没有使用Storyboard,则需要将BannerViewController设置为每个标签的父视图控制器。