共享 iAd 横幅

Shared iAd banner

我正在将我的应用程序中的 iAds 加载到应用程序委托中。它加载正常但是在视图控制器中,它不会显示。 我在应用程序委托中声明广告的代码是 var UIiAd: ADBannerView = ADBannerView() 我在视图控制器中的代码是

class HelpViewController: UIViewController, ADBannerViewDelegate {
    //MARK: - Properties
    var UIiAd: ADBannerView = ADBannerView()
    //MARK: - did something
    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(animated: Bool) {
        //super.viewWillAppear(animated)
        let ScreenHeight = UIScreen.mainScreen().bounds.height

        UIiAd.delegate = self
        UIiAd = self.appDelegate().UIiAd
        UIiAd.frame = CGRectMake(0, ScreenHeight - 50, 0, 0)
        UIiAd.hidden = true
        self.view.addSubview(UIiAd)
    }

    override func viewWillDisappear(animated: Bool) {
        UIiAd.delegate = nil
        UIiAd.removeFromSuperview()
    }



    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    //MARK: - iAd
    func bannerViewDidLoadAd(banner: ADBannerView!) {
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(1)
        UIiAd.hidden = false
        UIView.commitAnimations()
        print("Did load ad")
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        UIView.beginAnimations(nil, context: nil)
        UIView.setAnimationDuration(0)
        UIiAd.hidden = true
        UIView.commitAnimations()
        print("Did fail to receive ad with error \(error)")
    }

    //MARK: - Functions
    func appDelegate() -> AppDelegate {
        return UIApplication.sharedApplication().delegate as! AppDelegate
    }
}

问题似乎在于,bannerViewDidLoadAd 从未被调用过。横幅加载后如何取消隐藏?

我实际上已经解决了这个问题。您需要在应用程序委托中做的是 class AppDelegate: UIResponder, UIApplicationDelegate, ADBannerViewDelegate 然后你想做一个布尔值,如果广告被加载就会改变 然后你要添加函数 func bannerViewDidLoadAd(banner: ADBannerView!) { adLoaded = true } 这将在加载广告时更改值 然后在你的视图控制器中你想做

override func viewWillAppear(animated: Bool) {
    //super.viewWillAppear(animated)
    let ScreenHeight = UIScreen.mainScreen().bounds.height

    UIiAd.delegate = self
    UIiAd = self.appDelegate().UIiAd
    UIiAd.frame = CGRectMake(0, ScreenHeight - 50, 0, 0)
    canDisplayBannerAds = true
    if appDelegate().adLoaded == true {
    self.view.addSubview(UIiAd)
    }
}

这会将广告添加到您的视图控制器,如果广告未加载则不会