Pause and Resume Google Admob Banner ads when app enters background Objective c

Viewed 701

I am using Google Admobs with ad mediations in my iOS application. It seems like there is battery draining issue because google ads are running even when app is in background. Is there something that i have to do to Pause google Ads when app enters background and resume when app become active. Thanks in advance.

1 Answers

I have found a solution that has had mixed success for me. Hopefully someone can provide a more reliable solution. In AppDelegate.m I have this...

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self.viewController.bannerView removeFromSuperview];
    self.viewController.bannerView.delegate = nil;
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
     [self.viewController loadAdBanner];
}

This works for one of my apps. If I just call removeFromSuperview and don't set the delegate for the bannerView to nil, I still see background activity for my app when I look in the battery usage on my phone (running iOS 13.5.1). I have another app where I am doing basically the same thing but I still see background activity regardless. In both cases I am using AdMob 7.61.0.

My bannerView is declared like this:

@property(nonatomic, strong) GADBannerView *bannerView;

In MyViewController.m I have a loadAdBanner function that looks something like this...

- (void)loadAdBanner {
    ...
    [self.bannerView setDelegate:self];
    self.bannerView.rootViewController = self;
    [self.view addSubview:self.bannerView];
    GADRequest *request = [GADRequest request];
    [self.bannerView loadRequest:request];
    ...
}

I hope this helps.

Related