当没有广告显示时,让iAd横幅消失

时间:2015-03-12 03:29:31

标签: swift

Apple刚刚拒绝了我的应用,因为当没有广告显示时,横幅广告将变为白色且空白。显然,当没有广告时,您需要让横幅消失。他们向我发送了一个目标C的解决方案,但是我总是快速编写我的应用程序并且不熟悉Objective C.有人可以帮助我如何设置它以便在没有显示广告的情况下横幅消失。感谢帮助。这是我的代码:

import UIKit
import AVFoundation
import iAd

class StartUpViewController: UIViewController, ADBannerViewDelegate {

@IBOutlet var iAD: ADBannerView!


override func viewDidLoad() {
    super.viewDidLoad()

    iAD.delegate = self
    self.layoutScreen()

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

func layoutScreen() {
    var newBannerViewFrame = iAD.frame

    if iAD.bannerLoaded {
        // Show banner view
        let bannerHeight = iAD.frame.height
        let bannerY = self.view.bounds.height - bannerHeight
        newBannerViewFrame = CGRect(x: 0, y: bannerY, width: self.view.bounds.width, height: bannerHeight)
    } else {
        // Hide banner view
        newBannerViewFrame = CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: iAD.frame.height)
    }

    UIView.animateWithDuration(0.5, animations: {
        self.iAD.frame = newBannerViewFrame
        self.view.layoutIfNeeded()
    })
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.layoutScreen()
}

这就是Apple送我的地方:

(APPLE'评论) 后续步骤

请修改您的应用以实施横幅视图委托。这将在广告内容不可用时隐藏横幅广告。

为方便起见,此处包含示例代码段:

横幅视图代表在广告不可用时删除横幅视图:

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:    (NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
// assumes the banner view is at the top of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -   banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}

编辑:

我想我自己找到了一个解决方案。它似乎有效,但我想确定。任何人都可以查看此代码,并确认我是以正确的方式进行的。

override func viewDidLoad() {
    super.viewDidLoad()

    iAD.delegate = self
    iAD.hidden = true
    self.layoutScreen()

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

func layoutScreen() {
    var newBannerViewFrame = iAD.frame

    if iAD.bannerLoaded {
        // Show banner view
        let bannerHeight = iAD.frame.height
        let bannerY = self.view.bounds.height - bannerHeight
        newBannerViewFrame = CGRect(x: 0, y: bannerY, width: self.view.bounds.width, height: bannerHeight)
    } else {
        // Hide banner view
        newBannerViewFrame = CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: iAD.frame.height)
    }

    UIView.animateWithDuration(0.5, animations: {
        self.iAD.frame = newBannerViewFrame
        self.view.layoutIfNeeded()
    })
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    self.layoutScreen()
    iAD.hidden = false
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    iAD.hidden = true

}

1 个答案:

答案 0 :(得分:0)

没有尝试过,但是给定的代码可以翻译到以下内容中:

import UIKit
import iAd

class StartUpViewController: UIViewController, ADBannerViewDelegate {

    @IBOutlet var iAD: ADBannerView!
    var bannerIsVisible : Bool = false

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        iAD.delegate = self
        self.layoutScreen()
    }

    func layoutScreen() {
        var newBannerViewFrame = iAD.frame

        if (iAD.bannerLoaded) {
            // Show banner view
            let bannerHeight = iAD.frame.height
            let bannerY = self.view.bounds.height - bannerHeight
            newBannerViewFrame = CGRect(x: 0, y: bannerY, width: self.view.bounds.width, height: bannerHeight)
            self.bannerIsVisible = true
        } else {
            // Hide banner view
            newBannerViewFrame = CGRect(x: 0, y: self.view.bounds.height, width: self.view.bounds.width, height: iAD.frame.height)
            self.bannerIsVisible = false
        }

        UIView.animateWithDuration(0.5, animations: {
            self.iAD.frame = newBannerViewFrame
            self.view.layoutIfNeeded()
        })
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        if (self.bannerIsVisible) {
            UIView.beginAnimations("animateAdBannerOff", context: nil)
            // Assumes the banner view is at the top of the screen
            iAD.frame = CGRectOffset(banner.frame, 0, -iAD.frame.size.height);
            UIView.commitAnimations()
            self.bannerIsVisible = false
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

您需要添加一个布尔属性来跟踪您的横幅是否可见。

然后,Apple提供的方法来自ADBannerViewDelegate。在内部(广告无法加载时调用),您可以设置动画,以便在您的横幅可见时使其消失。提供的动画,如内部注释中所示,如果它位于视图的顶部,则移除横幅(如果不是这种情况,则需要调整更改框架位置的线)。