如何使用 AppDelegate 在视图之间共享 iAd 横幅 [英] How to share iAd banner between views using AppDelegate
问题描述
我希望在我的应用中实现 iAd.到目前为止,我已经设法使用下面的方法让它们在每个视图中正确显示/关闭.
I am looking to implement iAd in my app. So far I have managed to get them showing/dismissing correctly in each view using the below.
应用委托:
import UIKit
import iAd
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var adBannerView = ADBannerView()
视图控制器 1:
import UIKit
import iAd
class HomeScreenViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, ADBannerViewDelegate {
let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate //Creates reference to the AppDelegate
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
}
我的目标是在用户可以切换的多个视图中展示相同的广告.然而,在实践中,当您从一个视图切换到另一个视图时,广告会停止加载并且必须重新开始.有没有更简单的方法可以将这个 adBannerView 从一个 VC 传递到另一个 VC?
My goal is to have the same ad displaying in multiple views that the user can switch between. However, it seem's that in practice when you segue from one view to another, ad's stop loading and have to start again. Is there an easier way to pass this adBannerView from one VC to another?
任何帮助将不胜感激.谢谢!
Any help would be appreciated. Thanks!
推荐答案
in line
self.appDelegate.adBannerView = ADBannerView(frame: CGRect.zeroRect)
您正在创建一个新的 ADBannerView 实例并丢弃旧的实例.所以你没有共享视图对象,只是共享变量.删除此行.完成此操作后,
you are creating a new ADBannerView instance and throwing away the old one. So you are not sharing the view object, just the variable. Remove this line. After you've done this,
self.appDelegate.adBannerView.delegate = nil
没有任何意义,因为您稍后将其设置为 self
.也删除它.
does not make any sense, because you're setting it to self
later. Remove it, too.
另见 https://developer.apple.com/library/ios/technotes/tn2286/_index.html
这篇关于如何使用 AppDelegate 在视图之间共享 iAd 横幅的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!