Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone getting crazy hot and app lagging and crashing with Admob interstitial ads

I have some serious issued when it comes to my Admob implementation. I have followed official docs, but after a while playing my 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 am 100% sure it is due to a lot of Admob interstitial ads displaying. Since the app functions perfectly without them.

I will admit there is quite a lot of ads showing if you play for a while, but turning them off is not an option since it the main source of income on the app.

This is the code I use to load and display the ads:

- (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)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 after showing a large number of interstitial ads, is there some way of disposing them after displaying them?

like image 854
Peter Avatar asked Nov 06 '22 23:11

Peter


1 Answers

You need clear old interstitial before new request:

- (void)loadInterstitial {
    self.interstitial.fullScreenContentDelegate = nil;
    self.interstitial = nil;
    [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 = ad;
        self.interstitial.fullScreenContentDelegate = self;
    }];
}
like image 113
Cy-4AH Avatar answered Nov 15 '22 13:11

Cy-4AH