ios - Using iAds on iPad crashes after huge number of calls to bannerView:didFailToReceiveAdWithError: -
i have app uses xib defined adbannerview. if app runs on iphone (4 or 5) works expected, ads shown, banners hidden / shown etc.
however if app run on ipad crashes after repeatedly failing receive ad. examining call stack shows repeated calls bannerview:didfailtoreceiveadwitherror:
watching allocations while running on ipad shows continuous memory growth until crash.
messing network connectivity doesn't seem alter fact works on iphone not on ipad.
i read this question instead of using adbannerview in xib creates on fly , releases appropriately when ad fails load.
edit:
i changed devices setting in project file iphone universal. app not crash of course views 'messed up'. 1 option fix implement ipad idiom throughout app.
my questions -
what going on? no, really! i'm confused why there differing behaviour between devices.
should creating adbannerview programmatically? that kind of feels defeatist.
how can fix behaviour?
here code
#pragma mark adbannerviewdelegate - (void)bannerviewdidloadad:(adbannerview *)banner { [self showbanner]; } - (void)bannerview:(adbannerview *)banner didfailtoreceiveadwitherror:(nserror *)error { [self hidebanner]; } - (void)bannerviewactiondidfinish:(adbannerview *)banner { [self hidebanner]; } #pragma mark adbanner helpers - (void)hidebanner { cgrect hiddenframe = self.bannerdisplayframe; hiddenframe.origin.y = self.view.frame.size.height; [uiview animatewithduration:0.3f animations:^{ [self.adbannerview setframe:hiddenframe]; } completion:^(bool finished) { if (finished) { [self.adbannerview setalpha:0.0f]; } }]; } - (void)showbanner { [self.adbannerview setalpha:1.0f]; [uiview animatewithduration:0.3f animations:^{ [self.adbannerview setframe:self.bannerdisplayframe]; } completion:^(bool finished) { if (finished) { [nstimer scheduledtimerwithtimeinterval:60.0f target:self selector:@selector(hidebanner) userinfo:nil repeats:no]; } }]; }
it looks creating new iad banner views every time, suggested way use shared 1 throughout whole app. might reason of continuous memory growth in app , end warning apple servers if request ads many times. have @ here in apple's documentation more details iad best practices
this how implemented shared adbannerview, might of help.
appdelegate.h
@property (nonatomic, strong) adbannerview *adbanner;
appdelegate.m
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions { ... adbanner = [[adbannerview alloc] initwithframe:cgrectzero]; adbanner.delegate = self; adbanner.backgroundcolor = [uicolor clearcolor]; adbanner.autoresizingmask = uiviewautoresizingflexiblewidth | uiviewautoresizingflexibleheight | uiviewautoresizingflexibletopmargin; ... }
prefix.pch or better in header file included in prefix.pch
#define sharedadbannerview ((appdelegate *)[[uiapplication sharedapplication] delegate]).adbanner
and have implemented uiviewcontroller
category handle iads
@implementation uiviewcontroller (supportiad) -(void)bannerviewdidloadad:(adbannerview *)banner { sharedadbannerview.hidden = false; } -(void) bannerview:(adbannerview *)banner didfailtoreceiveadwitherror:(nserror *)error { sharedadbannerview.hidden = true; } //this method adds shared adbannerview current view , sets location bottom of screen //should work on devices -(void) addadbannerviewtobottom { sharedadbannerview.delegate = self; //position banner below screen sharedadbannerview.frame = cgrectmake(0, self.view.bounds.size.height, 0, 0); //height automatically set, raise view own height sharedadbannerview.frame = cgrectoffset(sharedadbannerview.frame, 0, -sharedadbannerview.frame.size.height); [self.view addsubview:sharedadbannerview]; } -(void) removeadbannerview { sharedadbannerview.delegate = nil; [sharedadbannerview removefromsuperview]; } @end
and in every viewcontroller going display iads, import category , in viewdidload:
- (void)viewdidload { ... [self removeadbannerview]; [self addadbannerviewtobottom]; ... }
Comments
Post a Comment