共享iAd横幅



我正在使应用程序中的iAd加载到应用程序委托中。它加载良好,但在视图控制器中不会显示。我在应用程序代理中声明广告的代码是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然后你想制作一个bool,如果广告被加载,它会发生变化然后您要添加函数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)
    }
}

这将把广告添加到您的视图控制器中。如果广告没有加载

最新更新