How to share iAd banner between views using AppDelegate

匿名 (未验证) 提交于 2019-12-03 08:54:24

问题:

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.

App Delegate:

import UIKit  import iAd @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate {      var window: UIWindow?      var adBannerView = ADBannerView()

View Controller 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     }

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!

回答1:

in line

self.appDelegate.adBannerView = ADBannerView(frame: CGRect.zeroRect)

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

does not make any sense, because you're setting it to self later. Remove it, too.

See also https://developer.apple.com/library/ios/technotes/tn2286/_index.html



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!