ADInterstitialAd is causing memory problems
I am developing an iPhone / iPad using the Sprite Kit and in between each round I load an interstitial ad.
The interstitial is loaded on GameViewController
top and sits on top skview
. I am using a series of watchers to trigger and cancel advertisements and it all works fine.
However, I noticed some serious memory issues and after 4 or 5 rounds the application will crash. It appears to be directly linked to the iAd interstitial site. I have attached my code and you can see that I am freeing objects, but the memory folder printing does not seem to crash. I am also using ARC.
Does anyone know what might be causing this problem? I read here: iAd and AdMob Heavy on Memory that the view webkit
seems to support its contents. I need to find a way to fix this, my code for mine GameViewController
looks like this:
#pragma mark - GAME LOAD
-(void)loadStartScreen{
_theView = (SKView *) self.view;
_theView.showsFPS = YES;
_theView.showsNodeCount = YES;
//Sprite Kit applies additional optimizations to improve rendering performance
_theView.ignoresSiblingOrder = YES;
// Create and configure the scene.
_theScene = [MainMenuScene sceneWithSize:_theView.bounds.size];
_theScene.scaleMode = SKSceneScaleModeAspectFill;
_theScene.backgroundColor = [UIColor grayColor];
// Present the scene
[_theView presentScene:_theScene];
// setup observer
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(requestFullScreenAd) name:@"requestAdvert" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showFullScreenAd) name:@"showAdvert" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cancelAdverts) name:@"cancelAdvert" object:nil];
}
#pragma mark - ADVERT CREATION AND SUPPORT
-(void)requestFullScreenAd {
// run the process on the main thread in a background queue
dispatch_async(bGQueue, ^{
if (_requestingAd == NO) {
_interstitial = [[ADInterstitialAd alloc]init];
_interstitial.delegate = self;
self.interstitialPresentationPolicy = ADInterstitialPresentationPolicyManual;
NSLog(@"Ad Request");
_requestingAd = YES;
}
});
}
-(void)showFullScreenAd{
if (_adLoaded) {
CGRect interstitialFrame = self.view.bounds;
interstitialFrame.origin = CGPointMake(0, 0);
_adView = [[UIView alloc] initWithFrame:interstitialFrame];
[self.view addSubview:_adView];
[_interstitial presentInView:_adView];
_button = [UIButton buttonWithType:UIButtonTypeCustom];
[_button addTarget:self action:@selector(closeAd:) forControlEvents:UIControlEventTouchDown];
_button.backgroundColor = [UIColor clearColor];
[_button setBackgroundImage:[UIImage imageNamed:kCloseAd] forState:UIControlStateNormal];
_button.frame = CGRectMake(10, 10, 40, 40);
_button.alpha = 0.75;
[_adView insertSubview:_button aboveSubview:_adView];
[UIView beginAnimations:@"animateAdBannerOn" context:nil];
[UIView setAnimationDuration:1];
[_adView setAlpha:1];
[UIView commitAnimations];
}
}
-(void)closeAd:(id)sender {
[UIView beginAnimations:@"animateAdBannerOff" context:nil];
[UIView setAnimationDuration:1];
[_adView setAlpha:0];
[UIView commitAnimations];
_adView=nil;
_requestingAd = NO;
_button = nil;
_interstitial.delegate = nil;
_interstitial = nil;
// notification for ad complete
[[NSNotificationCenter defaultCenter] postNotificationName:@"adClosed" object:nil];
}
-(void)cancelAdverts{
[_interstitial cancelAction];
_adView=nil;
_requestingAd = NO;
_button = nil;
_interstitial.delegate = nil;
_interstitial = nil;
}
#pragma mark - IAD DELEGATE
-(void)interstitialAd:(ADInterstitialAd *)interstitialAd didFailWithError:(NSError *)error {
[_interstitial cancelAction];
_adView=nil;
_requestingAd = NO;
_button = nil;
_interstitial.delegate = nil;
_interstitial = nil;
NSLog(@"Ad didFailWithERROR");
NSLog(@"%@", error);
// request another advert if it failed
//[self requestFullScreenAd];
}
-(void)interstitialAdDidLoad:(ADInterstitialAd *)interstitialAd {
if (interstitialAd.loaded) {
_adLoaded = YES;
[[NSNotificationCenter defaultCenter]postNotificationName:@"adLoaded" object:nil];
}
NSLog(@"Ad DidLOAD");
}
-(void)interstitialAdDidUnload:(ADInterstitialAd *)interstitialAd {
[self closeAd:nil];
NSLog(@"Ad DidUNLOAD");
}
-(void)interstitialAdActionDidFinish:(ADInterstitialAd *)interstitialAd {
[self closeAd:nil];
NSLog(@"Ad DidFINISH");
}
Then on my level the complete SKScene:
#pragma mark - SCENE APPEARS
-(void)didMoveToView:(SKView *)view {
// request an advert if advert removal is not purchased
if (![[[UserDetails sharedManager]iapAdsRemoved]boolValue]) {
// send request ad notification
[[NSNotificationCenter defaultCenter]postNotificationName:@"requestAdvert" object:nil];
// look for add loaded notification
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(adLoaded) name:@"adLoaded" object:nil];
// look for add completed
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(adShowCompleted) name:@"adClosed" object:nil];
}
// setup UI
[self createUI];
if (![[UnlockController sharedManager]allunlocksOpen]) {
// check all unlocks
[[UnlockController sharedManager]checkUnlocks:[[UserDetails sharedManager]userTotalScore]+[[UserDetails sharedManager]userLevelScore]];
// get the next unlock
[self getNextUnlockScore];
// set bar with correct increment
[unlockBar setBarValues:[[UserDetails sharedManager]userTotalScore]+[[UserDetails sharedManager]userLevelScore] increment:[[UserDetails sharedManager]userTotalScore] nextObject:nextScore];
}
else{
[self allUnlocksOpen];
}
// pre add button
preAdButtonPress = 3;
// variables
startCount = 0;
unlockItemsCount = 0;
allUnlocks = [[UnlockController sharedManager]finalUnlockOpen];
// start unlocks sequence
[self performSelector:@selector(runRewards) withObject:nil afterDelay:1.0];
}
-(void)willMoveFromView:(SKView *)view{
// cancel any adverts
[[NSNotificationCenter defaultCenter]postNotificationName:@"cancelAdvert" object:nil];
// remove observers
[[NSNotificationCenter defaultCenter]removeObserver:@"adClosed"];
[[NSNotificationCenter defaultCenter]removeObserver:@"adLoaded"];
}
source to share