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

2

6

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.

Billman answered 3/6, 2020 at 19:51 Comment(8)
I am experiencing the same issue - have you found a fix or workaround?Metathesize
I am seeing something similar with an app that uses SwiftUI. When the app enters the background, I remove the banner from the content view. It seems like this may have worked, but I am not completely convinced it always does the trick.Fortieth
I have the same issue starting from 2 weeks ago. Using the previous versions of admob didn't work so far.Exhortation
I am also using the same method @Fortieth suggested.. and i also do think its the right answer..Billman
In android there is something like adView Pause and Resume.Billman
Hi. I am facing same issue with my app. Would like to know how do you know it is Admob causing issue ?Undercharge
@Undercharge You can check it using Time Profiler InstrumentBillman
Latest admob library (19.5.0) on Android keeps using CPU even if app is in background, after pause() and even removing/destroying the AdView! Recurring issue that was discussed over and over years ago. Keeps coming back, never fixed. I'm slowly retiring ads from my apps because of this.Millham
F
4

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.

Fortieth answered 4/7, 2020 at 10:29 Comment(1)
I can confirm it works for me. Instead of overriding the AppDelegate methods, one can subscribe to didEnterBackgroundNotification in the controller itself.Isometric
N
0
NotificationCenter.default.addObserver(self, selector: #selector(appDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(appWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)


@objc func appDidEnterBackground() {
    adsManager?.pause()
}

@objc func appWillEnterForeground() {
    adsManager?.resume()
}

These two notifications trigger the functions when the app enters the background or foreground

Napiform answered 18/10 at 18:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.