user1751797
user1751797

Reputation: 15

iAd doesn't hide when it fails to load?

This seems like the dumbest question ever, but after wading through all of Apple's documentation and the useless online tutorials, I still can't figure out how to properly implement iAds into my application. So, my app starts off in a table view controller, and I have an iAd object underneath the navigation bar and above the table.

Now, in my code: (I also have the iAd framework added)

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

@interface MasterTableViewController : UITableViewController <ADBannerViewDelegate>
{
    IBOutlet ADBannerView *iAd;
}
@property(nonatomic, readonly, getter=isBannerLoaded) BOOL bannerLoaded;
@end

then in the .m file

#import "MasterTableViewController.h"

@interface MasterTableViewController ()
@end

@implementation MasterTableViewController
@synthesize bannerLoaded;

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
    if (!willLeave)
    {
        // nothing in this case thanks to ARC 
    }
    return YES;
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    iAd.hidden = NO;
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{   
    iAd.hidden = YES;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    for (int i = 0; i > 0; i++)
    {
        if (bannerLoaded)
        {
            iAd.hidden = NO;
        }
        else
        {
            iAd.hidden = YES;
        }
    }
}

Now, the problem is, when I test the app without internet connection the iAd does not load (obviously) BUT it also does not hide. So, at the top of the screen I'm left with a big white rectangle. Otherwise, the ad works fine when a connection is available. Does anyone have any ideas? Also - I just added the endless loops to see if they made a difference, those were completely on purpose lol.

Upvotes: 0

Views: 1892

Answers (2)

user1980555
user1980555

Reputation: 53

I'm assuming you have added your ADBannerView via the Storyboard as I can't see where you initialise the banner position.

  1. In storyboard, set the initial location just off screen.
  2. In "bannerViewDidLoadAd", animate the banner into view.
  3. In "bannerView: didFailToReceiveAdWithError:", animate the banner out of view.

There is a good example here https://developer.apple.com/library/ios/#documentation/UserExperience/Conceptual/iAd_Guide/WorkingwithBannerViews/WorkingwithBannerViews.html

Hope this helps. I have just implemented this but don't have access to my code at the moment.

It does bring up some errors in the simulator but works fine on a device.

I have tested this on iOS6 and works ok even if an iAd is displayed then the user loses the connection (so it looks like "bannerView: didFailToReceiveAdWithError:" is being called for me).

Upvotes: 1

Yaman
Yaman

Reputation: 3991

You have a property bannerLoaded for saving the state of your Ad, which is good.

At the very first, in your viewDidLoad method, iAd can't be loaded, so you have to set your property accordingly : self.bannerLoaded = NO;

Then, when you receive / fail to receive your ad, you need to update this property.

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    if (!self.bannerLoaded) {
        iAd.hidden = NO;
        self.bannerLoaded = YES;
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{   
    if (self.bannerLoaded) {
        iAd.hidden = YES;
        self.bannerLoaded = NO;
    }
}

Upvotes: 0

Related Questions