我的一个视图控制器上有一个广告横幅视图,作为我游戏的教学页面。当多次加载该视图控制器时,我收到以下错误。
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.
我在stackoverflow上搜索了这个错误,阅读了回复,但没有任何对我有用。我已经读过你打算发布广告横幅然后把它等等我已经收到这个错误了。我的“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;
}
}
答案 0 :(得分:0)
尝试从超级视图中删除iAd,然后再将其设置为nil:
-(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (_adBanner != nil) {
[_adBanner removeFromSuperview]; // <-new line
_adBanner.delegate = nil;
_adBanner.hidden = YES;
[_adBanner release];
_adBanner = nil;
}
答案 1 :(得分:0)
以下是我已在我的应用中使用的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;
}
}
答案 2 :(得分:0)
不是在单个类中初始化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];
}
}