How to split an iAd banner between views using AppDelegate

I want to implement iAd in my application. So far, I have managed to display / reject correctly in each view using below.

Application 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 show the same ad in multiple views that the user can switch between. However, it seems that in practice, when you move from one glance to another, the ad stops loading and should start again. Is there an easier way to transfer this adBannerView from one VC to another?

Any help would be greatly appreciated. Thank!

+4
source share
1

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

ADBannerView . , , . . , ,

self.appDelegate.adBannerView.delegate = nil

, self . .

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

0

All Articles