目前存在超过10个ADBannerView或ADInterstitialView实例:使用ARC

时间:2012-10-07 19:38:16

标签: objective-c automatic-ref-counting iad xcode4.5

我正在使用带有ARC的Xcode 4.5并使用iAd获取以下消息:

  

警告:目前存在超过10个ADBannerView或ADInterstitialView实例。这是对iAd API的误用,因此广告效果会受到影响。

我已经阅读了这篇文章:Iad WARNING: More than 10 instances of ADBannerView但是该代码没有看起来像我看来的ARC。

我的代码:

-(AppDelegate *) appdelegate {
return (AppDelegate *) [[UIApplication sharedApplication]delegate];
}

-(void) viewWillAppear:(BOOL)animated {
_UIiAD = [[self appdelegate]UIiAD];
_UIiAD.delegate = self;
adView = [[ADBannerView alloc]initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 460.0);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject: ADBannerContentSizeIdentifierPortrait];
[self.view addSubview:adView];
adView.delegate = self;
self.bannerIsVisible = NO;
}

- (void) viewWillDisappear:(BOOL)animated {
_UIiAD.delegate = nil;
_UIiAD = nil;
[_UIiAD removeFromSuperview];
}

-(void)bannerViewDidLoadAd:(ADBannerView *)banner {

if(!self.bannerIsVisible) {
    [UIView beginAnimations:@"animateAdBannerOn" context:NULL];
    //banner is invisible now and moved out of the screen
    banner.frame = CGRectMake(0.0, 5.0, banner.frame.size.width, banner.frame.size.width);
    [UIView commitAnimation];
    self.bannerIsVisible = YES;

    NSLog(@"bannerViewDidLoadAd is working!");
}
}

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {
if (self.bannerIsVisible) {
    [UIView beginAnimations:@"animateAdBannerOff" context:NULL];
    //banner is visible and we move it out of the screen due to connection issues
    banner.frame = CGRectMake(0.0, -50.0, banner.frame.size.width, banner.frame.size.width);
//        [UIView commitAnimation];
    self.bannerIsVisible = NO;

    NSLog(@"didFailtoReceiveWithError is working");
}
}

-(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
NSLog(@"Banner view is beginning an ad action");

BOOL shouldExecuteAction = YES;
if (willLeave && shouldExecuteAction) {
    // stop all interaction process in the app. Use this to stop sounds or video so the ad can display properly
    // [video pause];
    // [sound pause];
}
return shouldExecuteAction;
}

-(void)bannerViewActionDidFinish:(ADBannerView *)banner {
// resume everything you've stopped
// [video resume];
// [audio resume];
}

1 个答案:

答案 0 :(得分:2)

由于您首先从ivar中清除广告,因此您永远不会从超级视图中删除广告。尝试重写如下:

- (void) viewWillDisappear:(BOOL)animated {
[_UIiAD removeFromSuperview];
_UIiAD.delegate = nil;
_UIiAD = nil;
}