【问题标题】:Shared iAd banner共享 iAd 横幅
【发布时间】:2016-01-04 01:22:34
【问题描述】:

我正在将我的应用中的 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 从未被调用过。如果横幅加载,我将如何取消隐藏?

【问题讨论】:

标签: ios swift iad


【解决方案1】:

我实际上已经解决了这个问题。您需要在应用程序委托中做的是 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)
    }
}

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

【讨论】:

  • 这是错误的。 canDisplayBannerAds = true 实际上是在创建一个全新的ADBannerView,你不应该静态设置你的.frame
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-25
  • 1970-01-01
相关资源
最近更新 更多