5

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Bogdan Bogdanov
  • 882
  • 11
  • 36
  • 79
  • Hello, I am trying to use it, too. I've found the following: https://developer.apple.com/library/prerelease/ios/documentation/UserExperience/Reference/ADBannerViewDelegate_Ref/ – Mr. T Jul 11 '14 at 19:48
  • add the delegate on top of your class by ", ADBannerViewDelegate" instead of the old way "" – Mr. T Jul 11 '14 at 19:56
  • I would recommend you to use closures for your UIView animations: `UIView.animateWithDuration(1) { /* animations ... */ }` – return true Oct 25 '14 at 22:50
  • Please verify your chosen answer as Mr. T's answer results in a duplicate ADBannerView – chrisamanse Jun 04 '15 at 03:23

3 Answers3

17

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!

Community
  • 1
  • 1
Mr. T
  • 744
  • 8
  • 13
  • I am not able to hide ad using this. self.adBannerView.hidden = true Any idea? – Rohit Goyal Sep 09 '14 at 11:55
  • @RohitGoyal I've tested it another time and it still is working perfect for me with Xcode 6.0 (6A313). You could check if self.adBannerView != null and debug it. Without any code, we cannot help sorry. – Mr. T Sep 11 '14 at 17:56
  • Wouldn't this result into two `ADBannerView`s? – chrisamanse Jun 04 '15 at 03:21
3

If you are using iOS 7, extension methods and properties have been added to UIViewController to support handling of iAd:

See

https://developer.apple.com/library/prerelease/ios/documentation/iAd/Reference/UIViewController_iAd_Additions/index.html

To show an iAd you first need to add the iAd framework, go to the projects properties, general tab, add the iAd.framework in the Linked framework and libraries section.

In your view controller, import iAd to access the iAd extensions. And finally in viewDidLoad, set self.canDisplayBannerAds = true.

To remove ads, set canDisplayBannerAds to false

Note there is no need to create an AdBannerView in the story board or programmatically and there is no need for your view controller to implement the AdViewDelegate.

import UIKit
import iAd

class ViewController : UIViewController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        //That's it
        self.canDisplayBannerAds = true
    }
}
Pig Dog Bay
  • 149
  • 1
  • 3
1

Mr. T answer contains a lot of useless code.

All you need is this part to show ads in your controller:

override func viewDidLoad() {
      super.viewDidLoad()

      canDisplayBannerAds = true
}

And when you don't need ads, you canDisplayBannerAds = false.

What it does — wrapping your controller into another controller with ad banner at the bottom. This feature is available since iOS7.

It's not possible to get delegate messages with it, so if you need it — you should replace it with ADBannerView instance variable.

Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • No delegate methods? Well you can do it that way you suggest, but then you display an ad without finished loading - not that nice for the user. And if the user tap on the ad, you have to stop something in your app or game, thats possible with delegate methods only. – Mr. T Feb 26 '15 at 15:25
  • @Mr.T Without finished loading? What does it mean exactly? – Shmidt Feb 26 '15 at 15:32
  • @Mr.T Your downvoting shows that you don't understand what ```canDisplayBannerAds``` is. – Shmidt Feb 26 '15 at 15:39
  • 1
    Upvoted this one, since this should do the job. Adding another `ADBannerView` in the storyboard would result in two `ADBannerView`s, which Apple may reject since it violates its guidelines. From what I understand from Apple's documentation, setting `canDisplayBannerAds` to `true` would already let the `UIViewController` handle showing/hiding of ads. I've only tried the one-line code, so, please tell me if what I assume is wrong. – chrisamanse Jun 04 '15 at 03:17
  • 1
    @Christophr it's exactly as you wrote. – Shmidt Jun 04 '15 at 03:31