iosxcodeswiftiad

Check to see if iAd is available


I want to display iAd in a popover. I use a shared instance class to call the displayAd method. here is my shared instance 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) {}
        }
}

I use the displayAd function to display a popover (which contains PopOverVC which is an iAd) and then this is the 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)
    }

}

Now, what I want to do is call displayAd method from some other View Controller and then check if iAd is available and then show it. Right now my code shows the PopOver and then check for availability of iAd and if not available, it closes. I don't want to show PopOver unless the iAd is available.

Is there anyway to achieve this?

thanks


Solution

  • You can simply ask the ADBannerView with the bannerLoaded property:

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

    However, as Daniel pointed out above, you have six weeks to ship a replacement for your iAd code, so I very strongly recommend you work on that instead!