测试iAd很好,但实时iAd失败了

时间:2014-04-26 06:30:02

标签: ios iphone objective-c iad

当我的应用程序处于开发阶段时,iAds运行良好。每隔30秒我就会打电话给#34; bannerViewDidLoadAd"或者#34; didFailToReceiveAdWithError"我准备应用程序来处理任何回调。我得到绿色选中标记"您已经连接到iAd的应用网络"和其他测试广告。

现在该应用程序已启用,它只会获得" didFailToReceiveAdWithError"并且从不加载广告。 我在插入Xcode Organizer控制台的手机上运行应用程序的已发布版本,我看到在#34; didFailToReceiveAdWithError"中打印的NSLog。

iAd Portal不会显示任何请求,但会列出0个请求。

我已经使用开发配置文件再次从XCode将其构建到我的手机上,并且它再次正常工作。我删除了该应用,关闭了我的手机,退出了我的iTunes Apple ID,并从App Store重新下载了应用,但广告仍然每次都失败。

以下是我如何获得广告代码:

在我的rootViewController中,用户选择开始一个新游戏,并为新视图设置动画:

    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包括:

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

GamePlayViewController.m包括:

 - ADBannerView *_bannerView;

一旦用户进入GamePlayViewController.m,就会在viewDidLoad中触发动画,一旦该动画完成,就会调用一个广告:

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

    [self.view bringSubviewToFront:_bannerView];
}

除了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;
}

也许我正在做的事情是错的,或者我应该在rootViewController本身上播放广告,但是此代码适用于测试iAd,因此我不确定为什么它不能与App Store一起使用该应用程序的版本。

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

我没看过你的代码,但我也观察到了这一点。有时苹果公司没有出售任何广告,因此没有任何广告可供您展示。考虑使用广告聚合器。我使用https://github.com/larsacus/LARSAdController但修改它以按我想要的方式展示广告。

我在博客文章中介绍了这一点:http://www.notthepainter.com/iad-admob-integration-with-a-dynamic-uiview/


这是博客条目的文本,所以它保留在这里,以防万一我的博客有一天消失。


我用iAds发布了2个应用程序。我使用Apple的BannerView示例代码来实现这一点。基本上,在您的委托中,您没有将root设置为预期的根UIViewController,而是将root设置为包含真实根的BannerView。当iAd可用时,主视图会缩小,iAd会显示在底部。当广告不可用时,您的视图会扩展为“正常”尺寸。

这在测试中非常有效,所以我将这两个应用程序发布到应用程序商店。他们还做完了吗?和Wheelin。然而,当我第一次从商店下载版本时,我很惊讶地看到没有广告。事实证明,至少现在,iAd的填充率非常糟糕。所以当iAd不可用时,我想展示另一个广告。

我找到了LARSAdController,这是larsacus在GitHub上的一个开源项目。除了一件事,他使广告整合变得非常容易。当您沿着他的快速发展路线前进时,您会看到覆盖您视图的广告,但不会缩小以适应广告。这是一个完全合理的设计决定,而不是我想要的。

所以我决定修改Apple的BannerView以使用LARSAdController。这很简单。

您要做的第一件事就是从BannerView的.h文件中删除iAd,并在LARS TOLAdViewController类中删除广告。

#import "TOLAdViewController.h"

#define KVO_AD_VISIBLE @"KVO_AD_VISIBLE"

@interface BannerViewController : TOLAdViewController

(现在暂时忽略KVO_AD_VISIBLE定义,我将在稍后介绍。)在.m文件中也删除iAd并进行以下更改:

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

我们将_bannerView从ADBannerView更改为普通的旧UIVIew。 ADBannerView还有一个bannerLoaded属性,我们必须用我们的isLoaded布尔值替换它。 initWithContentViewController也很容易修改。

// 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
}

注意注释掉的部分。如果您使用应用内购买将广告支持版本转换为广告免费版本,则可以直接在那里执行此操作。

在方法结束时,请使用键值观察(KVO)来观看LARS并查看广告的投放或删除时间。 (我可能会在未来的博客文章中介绍KVO。)

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

观察代码:

- (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];
}

我们保存新广告的框架并更新isLoaded变量。 (原本以为我需要调用setNeedsLayout和layoutIfNeeded但实际上我没有。)viewDidLayoutSubviews的mod也非常简单。唯一奇怪的部分是删除对ADBannerContentSizeIdentifierPortrait和ADBannerContentSizeIdentifierLandscape的引用,只用一行替换所有引用:

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

稍后你会使用新的isLoaded变量

if (isLoaded) {

不要忘记在dealloc中清理你的观察者:

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

剩下的就是告诉LARS您的应用代表中的广告:

[[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];

就是这样。现在,您的应用应展示iAD或AdMob广告,您的视图将缩小以适应它们。

当然有一个错误,我不知道这是在AdMob服务器还是在LARS中,但是当你在iPhone上处于横向模式时,广告的大小和报告的大小不同,在底部留下一个黑条屏幕。我已经对它进行了larsacus,当我知道更多时,我会更新这篇文章。

我已经分叉了LARSAdController,并在我的github上的一个完整项目中提交了上面的示例代码。

答案 1 :(得分:0)

几天后,我准备好寻求支持,然后iAds开始工作了。所以我想答案是,应用程序上线和填充该应用程序的广告之间可能存在延迟。

最初,当广告开始进入时,填充率不会高于50%,我只看到iTunes广告的一个广告,但至少广告已开始有效。现在,填充率高达67%,而且我看到广告的种类更多。