I have an app with multiple UITableViews and am in the process of implementing iADs. Per the Apple documentation (http://developer.apple.com/library/ios/#technotes/tn2286/_index.html#//apple_ref/doc/uid/DTS40011212) I have created a shared banner that belongs to my app delegate and the application delegate is also the banner's delegate. This works well and the ads show nicely across the various view controllers AFTER the banner is loaded and the user switches screens.
The problem is that there is no ad seen on the first viewController that comes up because the viweWillAppear method of the view controller (where I am calling my "fixUpAdView" method) comes before the banner is loaded.
I guess the part that I am not getting is this (from the apple documentation): "Have your application delegate tell the current view controller if it should show or hide the banner. You can use UINavigationControllerDelegate or UITabBarControllerDelegate protocol to push the banner to show it." I understand that I need to be putting something into my bannerViewDidLoadAd and failToReceive methods but am a bit confused as to how to do this.
I do not want the ad to show on all of my view controllers (just 6 of them) and I also have several modal views in the app (no ad on any of these).
Here is some of my code: In my appDelegate:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(@"bannerViewDidLoadAD");
if (!_adBannerViewIsVisible)
_adBannerViewIsVisible = YES;
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"BannerAd didfailtoreceive");
if (_adBannerViewIsVisible)
_adBannerViewIsVisible = NO;
}
- (ADBannerView *)sharedAdBannerView
{
if (_sharedAdBannerView == nil) {
Class classAdBannerView = NSClassFromString(@"ADBannerView");
if (classAdBannerView != nil) {
_sharedAdBannerView = [[classAdBannerView alloc] initWithFrame:CGRectZero];
[_sharedAdBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:
ADBannerContentSizeIdentifier320x50,
ADBannerContentSizeIdentifier480x32, nil]];
[_sharedAdBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
[_sharedAdBannerView setFrame:CGRectOffset([_sharedAdBannerView frame], 0,
-(iAD_BANNER_HEIGHT))];
[_sharedAdBannerView setDelegate:self];
}
}
return _sharedAdBannerView;
}
In my view controller:
- (void)viewWillAppear:(BOOL)animated {
if ([[AppDelegate ad] shouldShowAds]) {
if (!self.contentView) {
self.contentView = [[UIView alloc] initWithFrame:[[self view] bounds]];
[self.view addSubview:_contentView];
}
[self.contentView addSubview:topView];
[self fixupAdView];
[self.view addSubview:[[AppDelegate ad] sharedAdBannerView]];
}
[super viewWillAppear:NO];
}
#pragma mark
#pragma mark iADS
- (void)fixupAdView {
if ([[AppDelegate ad] sharedAdBannerView] != nil) {
[[[AppDelegate ad] sharedAdBannerView] setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];
[UIView beginAnimations:@"fixupViews" context:nil];
if ([[AppDelegate ad] adBannerViewIsVisible]) {
CGRect adBannerViewFrame = [[[AppDelegate ad] sharedAdBannerView] frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = 0;
[[[AppDelegate ad] sharedAdBannerView] setFrame:adBannerViewFrame];
CGRect contentViewFrame = _contentView.frame;
contentViewFrame.origin.y = iAD_BANNER_HEIGHT;
contentViewFrame.size.height = self.view.frame.size.height -
iAD_BANNER_HEIGHT;
_contentView.frame = contentViewFrame;
}
else {
CGRect adBannerViewFrame = [[[AppDelegate ad] sharedAdBannerView] frame];
adBannerViewFrame.origin.x = 0;
adBannerViewFrame.origin.y = -(iAD_BANNER_HEIGHT);
[[[AppDelegate ad] sharedAdBannerView] setFrame:adBannerViewFrame];
CGRect contentViewFrame = _contentView.frame;
contentViewFrame.origin.y = 0;
contentViewFrame.size.height = self.view.frame.size.height;
_contentView.frame = contentViewFrame;
}
[UIView commitAnimations];
}
}
Using NSNotificationCenter to solve this problem worked like a charm and now my iAds are coming up as soon as they are loaded - yay! If anyone else needs this here is the extra code I input: (in my appDelegate.m)
- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
NSLog(@"bannerViewDidLoadAD");
if (!_adBannerViewIsVisible) {
_adBannerViewIsVisible = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:@"adjustAdBannerView" object:nil];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"BannerAd didfailtoreceive");
if (_adBannerViewIsVisible) {
_adBannerViewIsVisible = NO;
[[NSNotificationCenter defaultCenter] postNotificationName:@"adjustAdBannerView" object:nil];
}
}
and in my View Controller (in viewWillAppear):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(fixupAdView)
name:@"adjustAdBannerView"
object:nil];