2

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.

Luc
  • 16,604
  • 34
  • 121
  • 183

4 Answers4

10

I know I am too late for the answer but for people who might visit this thread for answer. this might help them

in header file

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

@interface ListViewController : UITableViewController <ADBannerViewDelegate>

@property (nonatomic, strong) ADBannerView *bannerForTableFooter;
@property (nonatomic) BOOL bannershouldBeVisible;

@end

in .m file

-(ADBannerView *)bannerForTableFooter
{
    if (!_bannerForTableFooter) {
        _bannerForTableFooter = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
        _bannerForTableFooter.delegate = self;
    }
    return  _bannerForTableFooter;
}

in ViewDidLoad

self.bannerForTableFooter.backgroundColor = [UIColor clearColor];

and then add these tableView datasource methods

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (self.bannershouldBeVisible) {
        return self.bannerForTableFooter.frame.size.height;
    }
    else
    {
        return 0;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (self.bannershouldBeVisible) {
        return self.bannerForTableFooter;
    }
    else
    {
        return [[UIView alloc] initWithFrame:CGRectZero];
    }
}

#pragma mark - iAd
-(void)bannerViewDidLoadAd:(ADBannerView *)bannerShown
{
    self.bannershouldBeVisible = YES;
    [self.tableView reloadData];
}

-(void)bannerView:(ADBannerView *)bannerShown didFailToReceiveAdWithError:(NSError *)error
{
    self.bannershouldBeVisible = NO;
    [self.tableView reloadData];
}
Taseen
  • 1,213
  • 1
  • 11
  • 18
6

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.

Community
  • 1
  • 1
NJones
  • 27,139
  • 8
  • 70
  • 88
  • I tested the table footer approach, but when the view appears the banner is somewhere near the middle of the screen. I need to scroll the tableview to have it moved to the bottom of the screen. – Luc Jul 10 '12 at 06:52
5

There is better way. From here: https://github.com/romaonthego/RETableViewManager/issues/50

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect frame = self.iAd.frame;
    frame.origin.y = scrollView.contentOffset.y + scrollView.frame.size.height-_iAd.frame.size.height; // Move view to the bottom on scroll
    self.iAd.frame = frame;
    [scrollView bringSubviewToFront:self.iAd];
}

Upd. There is better way to show iAd banner at the bottom since iOS7.

- (void)viewDidLoad
{
[super viewDidLoad];
self.canDisplayBannerAds = YES;
...
}
Shmidt
  • 16,436
  • 18
  • 88
  • 136
  • I've been using this method but lately in iOS 8.1 I am profiling my app and this line "self.iAd.frame = frame" has a huge performance hit! The screen is lagging when I scroll. I am not sure I saw this in iOS 7. – Maziyar Nov 04 '14 at 11:17
  • Thanks Shmidt. Is this working when you have a UITableView and want to keep it at the bottom of the page? – Maziyar Nov 04 '14 at 14:18
  • Yes, it is. And this line means you don't need to have ADBannerView. It will install its own. – Shmidt Nov 04 '14 at 14:20
  • Don't forget to use the #import in the .h file – Bilbo Baggins Feb 20 '15 at 10:30
4

From iOS 7 onwards none of these things are needed. All you have to do, as @Shmidt implied in his comment, is set self.canDisplayBannerAds = true in your viewDidLoad and everything else is automatically handled. This works beautifully with Table Views too.

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84
LNI
  • 2,935
  • 2
  • 21
  • 25
  • hey there. Welcome here, but this is not a good answer... You could have upvoted the comment you speak of, or even cite it in your answer, but as of now, you seem to cite a comment in an answer, which is not the point of an answer... – Félix Adriyel Gagnon-Grenier Jan 28 '15 at 17:26
  • Hi Felix, understood. Schmidt's answer was not clear, it did not clearly state that scrollview method was now redundant. That's why I thought it would be worthwhile having this as a standalone answer too. Also I don't have enough points to upvote yet, so I couldn't have upvoted the comment even if it was clear. If for no one else, this will be a useful answer/reminder for me. :-) Thanks for the welcome! – LNI Jan 29 '15 at 02:41
  • 1
    My pleasure. See how the edit by skrrgwasme (what a... name!) made your answer much better: removing noise (`For newbies like me`) and formatting code may seem trivial but now this answer have value by itself, even if short. – Félix Adriyel Gagnon-Grenier Jan 29 '15 at 17:28