After a while playing my iPhone game the phone starts to get really hot, and a while after that the app suddenly gets really slow and laggy, before eventually crashing.
I have followed official docs when implementing Admob ads.
I know for a fact the problem is Admob, since the app functions perfectly when turning off ads.
I know there is quite a lot of ads showing if you play for a while, but limiting the amount of ads displaying will only delay the lag and crash, and will not fix the underlying issue.
This is the code I use to load and display the ads:
- (void)viewDidLoad {
[super viewDidLoad];
[self loadInterstitial];
}
- (void)loadInterstitial {
[GADInterstitialAd loadWithAdUnitID:@"ca-app-pub-xxx/xxx" request:[GADRequest request] completionHandler:^(GADInterstitialAd *ad, NSError *error) {
if (error) {
NSLog(@"Failed to load interstitial ad with error: %@", [error localizedDescription]);
return;
}
self.interstitial.fullScreenContentDelegate = nil;
self.interstitial = ad;
self.interstitial.fullScreenContentDelegate = self;
}];
}
- (void)viewDidAppear:(BOOL)animated {
adCount = [[NSUserDefaults standardUserDefaults] integerForKey:@"adCount"] + 1;
[[NSUserDefaults standardUserDefaults] setInteger:adCount forKey:@"adCount"];
[[NSUserDefaults standardUserDefaults] synchronize];
if (adCount > 11) {
[self displayInterstitial];
}
}
- (void)displayInterstitial {
if (self.interstitial) {
[self.interstitial presentFromRootViewController:self];
adCount = 0;
[[NSUserDefaults standardUserDefaults] setInteger:adCount forKey:@"adCount"];
[[NSUserDefaults standardUserDefaults] synchronize];
} else {
NSLog(@"Ad wasn't ready");
}
}
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
[self loadInterstitial];
}
Nothing special. To me it seems like the memory gets overloaded or too many leaks after displaying enough interstitial ads.