Search code examples
objective-ciosxcodestoryboardiad

Add iAd banner in UItableView with storyboard


I do not manage to have iAd working in storyboard, within a UITableViewController.

This is basically what I have done so far: In storyboard, I have dragged an iAd banner view in the bottom of the scene of my UITableViewController. (I was not able to set the banner in the view itself as storyboard prevents it. I could only drag the iAd object next to the first responder and UITableViewController's icons.)

In the header of the UITAbleViewController, I have:

#import <UIKit/UIKit.h>
#import "ListViewController.h"
#import <iAd/iAd.h>

@interface ListViewController : UITableViewController <ADBannerViewDelegate>{
    bool bannerIsVisible;
}

@property (strong, nonatomic) IBOutlet ADBannerView *iAd;

@end

In the implementation of the UITableViewController, I have:

- (void)viewDidLoad
{
  [super viewDidLoad];

  iAd.delegate = self;
  // iAd.frame = CGRectMake(0.0,200.0,iAd.frame.size.width,iAd.frame.size.height); // does not work
  //  self.tableView.tableFooterView = iAd;   // Display the banner at the middle of the screen (depending upon the number item of the table)

  // Do not know how to have the default banner to appear at the very bottom of the screen (behind the tab bar) when the view is loaded ?

} 


- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
  NSLog(@"didFailToReceiveAdWithError");
  if (bannerIsVisible)
  {
    [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
    banner.frame = CGRectMake(0.0,350.0,banner.frame.size.width,banner.frame.size.height);
    [UIView commitAnimations];
    bannerIsVisible = NO;
  }
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
  NSLog(@"bannerViewDidLoadAd");
  if (!bannerIsVisible)
  {
    [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
    banner.frame = CGRectMake(0.0,317.0,banner.frame.size.width,banner.frame.size.height);
    [UIView commitAnimations];
    bannerIsVisible = YES;
  }
}

The idea is to have the banner appearing at the bottom of the screen and to show it above the tab bar is the banner load is succesfulf.

The problem I have: with this configuration I can see that sometimes the method "didFailToReceiveAdWithError" is called and sometimes the "bannerViewDidLoadAd" one, but in each case the banner is not shown.


Solution

  • Your problem is a common one. It's not happening due to storyboard but rather is happening due to the fact the you are using a UITableViewController. UITableViewControllers give you a lot for free, but they do require a few things. One of those 'things' is that they demand their view be a UITableView.

    You have a few options. Here are a couple of the preferred ones.

    1) Add your iAd as the tableFooterView. This requires some extra work to keep it onscreen while scrolling as out lined in my answer to this question.

    2) Convert your UITableViewController subclass to be a UIViewController subclass. This is interestingly what the OP of the question linked above ended up doing. Essentially then you can animate the (now a subview) tableView's frame while animating the iAd. And then it will work as expected. I outlined the basic steps for converting to a UIViewController subclass in a .xib file in this answer, most of it should be applicable to you.