Search code examples
swiftiadadsadbannerview

Swift - ADBannerView


I tried to implement ADBannerView with the old way like Objective C but unsuccessfull. Everythings work but the advertisments didn't show up, it stays a blank field.

func bannerViewDidLoadAd(banner: ADBannerView!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    banner.alpha = 1
    UIView.commitAnimations()
}

func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
    UIView.beginAnimations(nil, context: nil)
    UIView.setAnimationDuration(1)
    banner.alpha = 0
    UIView.commitAnimations()
}

Anyone who already tried iAd on Swift?


Solution

  • I've found a solution, how to implement it. (You can use inside each method "banner.alpha 1.0" or other things, too.)

    //import ... your normal imports as UIKit etc.
    import iAd
    
    class YourClassViewController: UIViewController, ADBannerViewDelegate {
    
       @IBOutlet var adBannerView: ADBannerView //connect in IB connection inspector with your ADBannerView
    
       override func viewDidLoad() {
          super.viewDidLoad()
    
          self.canDisplayBannerAds = true
          self.adBannerView.delegate = self
          self.adBannerView.hidden = true //hide until ad loaded
       }
    
       func bannerViewWillLoadAd(banner: ADBannerView!) {
          NSLog("bannerViewWillLoadAd")
       }
    
       func bannerViewDidLoadAd(banner: ADBannerView!) {
          NSLog("bannerViewDidLoadAd")
          self.adBannerView.hidden = false //now show banner as ad is loaded
       }
    
       func bannerViewActionDidFinish(banner: ADBannerView!) {
          NSLog("bannerViewDidLoadAd")
    
          //optional resume paused game code
    
       }
    
       func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {
          NSLog("bannerViewActionShouldBegin")
    
          //optional pause game code
    
          return true
       }
    
       func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) {
          NSLog("bannerView")
       }
    
       //... your class implementation code
    
    }
    

    See the following answer, on how to do it without IBBuilder!