Question

When my app was in development, iAds worked great. Every 30 seconds I would either get a call to "bannerViewDidLoadAd" or to "didFailToReceiveAdWithError" and I prepared the app to handle either callback. I get the green checkmark "You're connected to iAd's App Network" and the other test ads.

Now that the app is live, it only gets "didFailToReceiveAdWithError" and never loads an ad. I'm running the released version of the app on my phone plugged in to the Xcode Organizer Console, and I see the NSLog that prints within "didFailToReceiveAdWithError"

The iAd Portal doesn't show any requests though, it lists 0 requests.

I've built it to my phone again from XCode with the development profile and again it works as it should. I've deleted the app, shut down my phone, signed out of my iTunes Apple ID, and redownloaded the app from the App Store and still the ad fails every time.

Here's how I've got the ad coded:

In my rootViewController, the user chooses to start a new game, and I animate the new view:

    UIViewController *nextController = [[GamePlayViewController alloc] initWithNibName:@"GamePlayView" bundle:nil];
    [nextController performSelector:@selector(setDelegate:) withObject:self];

    nextController.view.frame = CGRectMake(0, 570, 320, 568);
    [self.view addSubview:nextController.view];

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.23];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];

    nextController.view.frame = CGRectMake(0, 0, 320, 568);

    [UIView commitAnimations];

    temporaryController = nextController;

GamePlayViewController.h includes:

 - #import <iAd/iAd.h>
 - @interface GamePlayViewController : UIViewController <ADBannerViewDelegate, UIDocumentInteractionControllerDelegate> {

GamePlayViewController.m includes:

 - ADBannerView *_bannerView;

Once the user is in GamePlayViewController.m, there is an animation triggered in viewDidLoad, and once that animation completes, an ad is called:

if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
        _bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
    } else {
        _bannerView = [[ADBannerView alloc] init];
    }
    _bannerView.delegate = self;

    [self.view bringSubviewToFront:_bannerView];
}

That's really all there is to it other than the callback methods for iAds.

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    NSLog(@"ad loaded!");

    _bannerView.hidden = NO;
    [self layoutAnimated:YES];
}

- (void)layoutAnimated:(BOOL)animated
{
    // As of iOS 6.0, the banner will automatically resize itself based on its width.
    // To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately.
    CGRect contentFrame = self.view.bounds;
    if (contentFrame.size.width < contentFrame.size.height) {
        _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    } else {
        _bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
    }

    CGRect bannerFrame = _bannerView.frame;
    if (_bannerView.bannerLoaded) {
        bannerFrame.origin.y = 0;
    } else {
        bannerFrame.origin.y = contentFrame.size.height;
    }


    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
        _contentView.frame = contentFrame;
        [_contentView layoutIfNeeded];
        _bannerView.frame = bannerFrame;
        [self.view addSubview:_bannerView];
    }];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"ad failed!");
    _bannerView.hidden = YES;
}

Maybe something I am doing is wrong or I should have the ad on the rootViewController itself, but this code works great with test iAds, so I'm not sure why it's not working with the App Store version of the app.

Thanks for any help!

Était-ce utile?

La solution 2

After a few days, I was getting ready to call for support, and then the iAds started working. So I guess the answer is that there is potential for a lag to occur between an app going live and ads populating for that app.

Initially when the ads started coming in, the fill rate wasn't much above 50% and I was only seeing the one ad for iTunes Radio, but at least the ads had started working. Now fill rate is up to 67% and I'm seeing a bit more of a variety with the ads.

Autres conseils

I've not looked at your code but I've observed this also. Sometimes Apple just hasn't sold any ads so there aren't any to display for you. Consider using an ad aggregator. I use https://github.com/larsacus/LARSAdController but modified it to display ads the way I want to.

I cover that in my blog entry: http://www.notthepainter.com/iad-admob-integration-with-a-dynamic-uiview/


Here is the text of the blog entry so it is preserved here, just in case my blog goes away someday.


I’ve released 2 apps both with iAds. I used Apple’s BannerView sample code to implement this. Basically, in your delegate you don’t set root to your expected root UIViewController but rather you set root to a BannerView which contains your real root. When an iAd is available, your main view shrinks and the iAd is displayed at the bottom. When an ad isn’t available, your view expands to its “normal” size.

This worked very well in testing so I released both apps to the app store. They’re Done Yet? and Wheelin. However, when I first downloaded the versions from the store I was quite surprised to see no ads ever. It turns out that at least right now, iAd had a pretty horrible fill rate. So I wanted to show another ad when an iAd wasn’t available.

I found LARSAdController, an open source project by larsacus on GitHub. He makes ad integration very easy except for one thing. When you go down his quick development route you get the ads covering your view, it doesn’t shrink to accommodate the ad. This is a completely reasonable design decision, just not one I wanted.

So I decided to modify Apple’s BannerView to use LARSAdController. It was pretty easy.

The first thing you do is remove iAd from BannerView’s .h file and ad in the LARS TOLAdViewController class.

#import "TOLAdViewController.h"

#define KVO_AD_VISIBLE @"KVO_AD_VISIBLE"

@interface BannerViewController : TOLAdViewController

(Just ignore the KVO_AD_VISIBLE define for now, I’ll cover that later.) In the .m file also remove iAd and make these changes:

@implementation BannerViewController {
    UIView *_bannerView;
    UIViewController *_contentController;
    BOOL isLoaded;
}

We changed _bannerView from an ADBannerView into a plain old UIVIew. ADBannerView also has a bannerLoaded property which we’ll have to replace with our isLoaded boolean. initWithContentViewController is also easy to modify.

// IAPHelper *sharedInstance = [//IAPHelper sharedInstance];
//if ([sharedInstance showBannerAds]) {
if (YES) {
    _bannerView = [[UIView alloc] initWithFrame:CGRectZero];
} else {
    _bannerView = nil;      // not showing ads since the user has upgraded
}

Notice the commented out section. If you are using in-app purchases to transform an ad supported version into an ad free version you can do that right there.

At the end of the method well use Key-Value-Observing (KVO) to watch LARS and see when an ad is served or removed. (I’ll probably cover KVO in a future blog entry.)

[[LARSAdController sharedManager] addObserver:self
                                   forKeyPath:kLARSAdObserverKeyPathIsAdVisible
                                      options:0
                                      context:KVO_AD_VISIBLE];

And the observing code:

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary *)change
                       context:(void *)context;
{
    if(context == KVO_AD_VISIBLE) {
        NSNumber *isVisible = [object valueForKey:kLARSAdObserverKeyPathIsAdVisible];

        if ([isVisible boolValue]) {
            _bannerView.frame = [[LARSAdController sharedManager] containerView].frame;
            isLoaded = YES;
        } else {
            isLoaded = NO;
        }
    }

    [self.view setNeedsLayout];
    [self.view layoutIfNeeded];
}

We save the frame of the new ad and also update the isLoaded variable. (Originally thought I would need to call setNeedsLayout and layoutIfNeeded but in practice I didn’t.) The mods to viewDidLayoutSubviews were also pretty straightforward. The only odd part was removing the references to ADBannerContentSizeIdentifierPortrait and ADBannerContentSizeIdentifierLandscape and just replacing that all with a single line:

bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];

And a few lines later you use the new isLoaded variable

if (isLoaded) {

Don’t forget to clean up your observer in dealloc:

-(void) dealloc;
{
    [[LARSAdController sharedManager]  removeObserver:self forKeyPath:kLARSAdObserverKeyPathIsAdVisible];
}

All that remains is to tell LARS about your ads in your app delegate:

[[LARSAdController sharedManager] registerAdClass:[TOLAdAdapterGoogleAds class] withPublisherId:@"a14e55c99c24b43"];
[[LARSAdController sharedManager] registerAdClass:[TOLAdAdapteriAds class]];

LARSBannerViewController *root = [[LARSBannerViewController alloc] initWithNibName:@"LARSBannerViewController" bundle:nil];

_bannerViewController = [[BannerViewController alloc] initWithContentViewController:root];

[self.window setRootViewController:_bannerViewController];

And that’s it. Now your app should show iAD or AdMob ads and your view will shrink to accommodate them.

Of course there’s a bug, I don’t know if this is in AdMob server or in LARS but when you are in Landscape mode on an iPhone the ad’s size and the reported size are different leaving a black bar at the bottom of the screen. I’ve pinged larsacus about it and will update this post when I know more.

I’ve forked LARSAdController and submitted the above sample code in a full project on my github.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top