0

I added the iAd framework. I drag and dropped the iAdBannerView onto the view controller. Then I set constraints for the banner. Next I connected the banner to the ViewController.swift file. After doing so I wrote this code :-

On top of file :

import iAd
class ViewController: UIViewController, UITextFieldDelegate, ADBannerViewDelegate 

{

Creating the outlet :

@IBOutlet var adBannerView: ADBannerView!

In viewDidLoad() :

self.canDisplayBannerAds = true
self.adBannerView.delegate = self
self.adBannerView.hidden = true

Next I created these two functions :

func bannerViewActionShouldBegin(banner: ADBannerView!, willLeaveApplication willLeave: Bool) -> Bool {

    return true

}

func bannerViewDidLoadAd(banner: ADBannerView!) {

    adBannerView.hidden = false
}

This is all I did. I didn't fill any form on iTunes connect or import any other framework related to iAds. This is literally all that I have done.

But yet when I ran the app on the simulator, the iAd banner did not show up. It did not even show the Ads by Apple. Nothing at all. It just blended in with the app. I ran the app on my phone, and still nothing. I uploaded the app to the AppStore, and I still don't see anything when I download my app from the AppStore. Any idea what went wrong here? Thanks in advance :)

Krish Wadhwana
  • 1,544
  • 2
  • 12
  • 24
  • u having problem in device or simulator. In India and some other country iAd wont work on device but it definately work on simulator – vaibby Dec 11 '15 at 12:58
  • @vaibby I am having a problem on both device and simulator – Krish Wadhwana Dec 11 '15 at 13:00
  • try working on constraints u set on adBannerView. Also check view height / width – vaibby Dec 11 '15 at 13:04
  • @vaibby I tried printing the frame of the banner, and it fell within the frame of the view, and right where it should be. But it doesn't appear for some reason. – Krish Wadhwana Dec 11 '15 at 13:06
  • You should also add the method `func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!)` and print the error, so you know what iAd complains about. As already suggested by vaibby, please check the dimensions of the ad banner view - see also my answer there http://stackoverflow.com/a/33945790/1396265 – Rainer Schwarze Dec 11 '15 at 13:13
  • @RainerSchwarze I had previously added that function, and I was getting an error repeatedly. Also, there is no problem with the constraints. – Krish Wadhwana Dec 11 '15 at 13:17
  • Which error did you get? – Rainer Schwarze Dec 11 '15 at 13:20
  • @RainerSchwarze I got the I error which I wanted to be printed if the banner view failed to load. – Krish Wadhwana Dec 11 '15 at 13:21
  • Can you copy&paste the exact error message? – Rainer Schwarze Dec 11 '15 at 13:23
  • See, all I did was write this code-`func bannerView(banner: ADBannerView!, didFailToReceiveAdWithError error: NSError!) { print("Error!") }` In the console I got the error - `Error!` – Krish Wadhwana Dec 11 '15 at 13:26

1 Answers1

0

Create a separate function and enter it into viewDidLoad (it helps organize). After you are done with that enter this in.

func iAD() {

    bannerView = ADBannerView(adType: .Banner)
    bannerView.translatesAutoresizingMaskIntoConstraints = false
    bannerView.delegate = self
    bannerView.hidden = true
    view.addSubview(bannerView)

    let viewsDictionary = ["bannerView": bannerView]
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[bannerView]|", options: [], metrics: nil, views: viewsDictionary))
    view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:[bannerView]|", options: [], metrics: nil, views: viewsDictionary))
}

func bannerViewDidLoadAd(banner: ADBannerView!) {
    bannerView.hidden = false
}

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

Then when you open it up you should get something like this

shim
  • 9,289
  • 12
  • 69
  • 108
Tuple Dev
  • 1
  • 1