iAd 横幅错误“iAd 实例过多”

Posted

技术标签:

【中文标题】iAd 横幅错误“iAd 实例过多”【英文标题】:iAd Banner Error "Too many instances of iAd" 【发布时间】:2014-07-12 05:57:27 【问题描述】:

我的一个视图控制器上有一个广告横幅视图,用作我的游戏的说明页面。当多次加载该视图控制器时,我收到以下错误。

WARNING: More than 10 instances of ADBannerView or ADInterstitialView currently exist. This is a misuse of the iAd API, and ad performance will suffer as a result. This message is printed only once.

我在 *** 上搜索了这个错误,阅读了回复,但对我没有任何帮助。我已经读到您打算发布广告横幅,然后将其归零等。我有并且仍然收到此错误。我的“viewWillAppear”和“viewWillDissapear”的代码如下。任何帮助将不胜感激!

-(void) viewWillAppear:(BOOL)animated 

[super viewWillAppear:animated];

_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
[self.view addSubview:_adBanner];

-(void) viewWillDisappear:(BOOL)animated 

[super viewWillDisappear:animated];

if (_adBanner != nil) 
    _adBanner.delegate = nil;
    _adBanner.hidden = YES;
    [_adBanner release];
    _adBanner = nil;


【问题讨论】:

【参考方案1】:

在将 iAd 设置为 nil 之前,尝试从其超级视图中移除 iAd:

-(void) viewWillDisappear:(BOOL)animated 
    [super viewWillDisappear:animated];

    if (_adBanner != nil) 
        [_adBanner removeFromSuperview]; // <-new line

        _adBanner.delegate = nil;
        _adBanner.hidden = YES;
        [_adBanner release];
        _adBanner = nil;

【讨论】:

嗨,我刚刚尝试使用此代码,但出现了同样的错误。 @user3752308 太可惜了,从超级视图中删除它是解决它的常用方法。您对viewWillAppearviewWillDisappear 的调用次数是否大致相同? 不知道如何检查,我只是对这个错误感到很困惑......所有其他有问题的人似乎都可以接受通常的诊断。【参考方案2】:

这里是 iAd 的代码,我已经在我的应用程序中使用,没有任何问题:

implementation ViewController

    ADBannerView *adView;
    BOOL bannerIsVisible;


- (void)viewDidLoad

    [super viewDidLoad];


    adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
    adView.frame = CGRectOffset(adView.frame, 0, -50);
    adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
    adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
    [self.view addSubview:adView];
    adView.delegate=self;
    bannerIsVisible=NO;


- (void)bannerViewDidLoadAd:(ADBannerView *)banner

    if (!bannerIsVisible)
    
        [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
        // banner is invisible now and moved out of the screen on 50 px
        banner.frame = CGRectOffset(banner.frame, 0, 50);
        [UIView commitAnimations];
        bannerIsVisible = YES;
    


- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error

    if (bannerIsVisible)
    
        [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
        // banner is visible and we move it out of the screen, due to connection issue
        banner.frame = CGRectOffset(banner.frame, 0, -50);
        [UIView commitAnimations];
        bannerIsVisible = NO;
    

【讨论】:

【参考方案3】:

不要在单个类中初始化adBannerView,而是在AppDelegate 中创建它。

我说:

@property (strong, nonatomic) ADBannerView *adBanner;

在我的AppDelegate.h 和:

self.adBanner = [[ADBannerView alloc] init];

在我的AppDelegate.m

然后在我放的单个班级中:

@property (strong, nonatomic) ADBannerView *adBanner;

在 .h 和 .m 中我放了:


    BOOL _bannerIsVisible;

@interface 的正下方和@implementation 中我放了:

#pragma mark Ads




- (WTMAppDelegate *) appdelegate 
    return (WTMAppDelegate *)[[UIApplication sharedApplication] delegate];


- (void)bannerViewDidLoadAd:(ADBannerView *)banner

    NSLog(@"Retreieved Ad");
    [self bringBannerBack];



- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error

    NSLog(@"Failed to retrieve ad");

    [self moveBannerViewOffScreen];


- (void)bringBannerBack




    [self.adBanner setAlpha:1];
    [self.adBanner setNeedsDisplay];
    [UIView beginAnimations:@"MoveView" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
    [UIView setAnimationDuration:0.1];
    self.adBanner.frame = CGRectMake(0, self.view.bounds.size.height - 50, 320, 50);
    [UIView commitAnimations];
    _bannerIsVisible = YES;
    [self.adBanner setAlpha:1];


//Move the banner off the screen.
- (void)moveBannerViewOffScreen


    [self.adBanner setAlpha:0];
    [self.adBanner setNeedsDisplay];
    _bannerIsVisible = NO;


在 viewWillAppear 和 ViewWillDisappear 中:

- (void) viewWillDisappear:(BOOL)animated


    self.adBanner.delegate=nil;
    self.adBanner=nil;
    [self.adBanner removeFromSuperview];





- (void) viewWillAppear:(BOOL)animated

    self.adBanner = [[self appdelegate] adBanner];
    self.adBanner.delegate = self;
    [self.view addSubview:self.adBanner];

    if (self.adBanner.bannerLoaded == NO)
        NSLog(@"NO initial banner loaded");
        [self moveBannerViewOffScreen];
     else 
        NSLog(@"Initial ad loaded");
        [self bringBannerBack];
    


【讨论】:

以上是关于iAd 横幅错误“iAd 实例过多”的主要内容,如果未能解决你的问题,请参考以下文章

在 UITableView 下方放置 iAd 横幅

iAd 默认横幅尺寸不正确

带有 iAd 横幅的 UITableView 上的奇怪溢出

使用故事板在 UItableView 中添加 iAd 横幅

iOS 在 iPad 详细拆分视图中更改 iAD 横幅大小

iAd 横幅显示在模拟器上,但不显示在设备上