检查 iAd 是否可用

Check to see if iAd is available

我想在弹出窗口中显示 iAd。我使用共享实例 class 来调用 displayAd 方法。这是我的共享实例 class :

    class Share : NSObject ,UIPopoverPresentationControllerDelegate {
        static let sharedInstance = Share()        

            func displayAd(sender:UIViewController) {

            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("PopOverVC") as UIViewController
            vc.preferredContentSize = CGSize(width: 310, height: 250)
            let navC = UINavigationController(rootViewController: vc)
            navC.modalPresentationStyle = UIModalPresentationStyle.Popover
            let popOver = navC.popoverPresentationController
            popOver?.delegate = self
            popOver?.sourceView = sender.view
            popOver?.sourceRect = CGRectMake(CGRectGetMidX(sender.view.bounds), CGRectGetMidY(sender.view.bounds),0,0)
            popOver?.permittedArrowDirections = UIPopoverArrowDirection(rawValue:0)
            navC.navigationBarHidden = true
            sender.presentViewController(navC, animated: true) {}
        }
}

我使用 displayAd 函数来显示弹出窗口(其中包含 PopOverVC,它是一个 iAd) 然后这是 PopOverVC class:

class PopOverVC: UIViewController,ADBannerViewDelegate {

    var ad = ADBannerView()
    @IBOutlet var Banner: ADBannerView!

    @IBAction func CloseBtn(sender: UIButton) {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewDidLoad() {
        Banner = ad
    }
func bannerViewDidLoadAd(banner: ADBannerView!) {
        sharedAd.hidden = false
    }

    func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
        sharedAd.hidden = true
        self.dismissViewControllerAnimated(true, completion: nil)
    }

}

现在,我想做的是从其他视图控制器调用 displayAd 方法,然后检查 iAd 是否可用,然后显示它。 现在我的代码显示 PopOver,然后检查 iAd 的可用性,如果不可用,它会关闭。 除非 iAd 可用,否则我不想显示 PopOver。

有没有办法实现这个?

谢谢

您可以简单地 ask the ADBannerViewbannerLoaded 属性:

Banner views automatically download new advertisements in the background. This property returns true if an advertisement is loaded; false otherwise.

但是,正如 Daniel 在上面指出的那样,you have six weeks 发布 iAd 代码的替代品,所以我非常强烈 建议您改用它!