iOS AdMob memory leak? - ios

I just started using AdMob but I noticed that, after running it for about an hour, it's accumulated 50MB! Yikes. I thought about releasing it but I can't since I am using ARC. Any ideas? I'm using the getting started code provided by google:
GADBannerView *bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
CGRect newFrame = CGRectMake(self.scroller.frame.origin.x,self.scroller.frame.origin.y + 70,self.scroller.frame.size.width,self.scroller.frame.size.height);
[self.scroller setFrame:newFrame];
bannerView_.adUnitID = #"XXXXX";
bannerView_.rootViewController = self;
[bannerView_ setFrame:CGRectMake(0,
20,
bannerView_.bounds.size.width,
bannerView_.bounds.size.height)];
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];

I had the same problem.
When receiving a new ad, you must remove the previous ad from the parent view.
Otherwise, they are superimposed on each other and consumes memory.
So, after receiving more than 15 advertisements, the percentage of allocated memory remained constant.
Hoping that this will help you.
- ( void )displayBanner:( UIView * )banner
{
UIView * oldBanner = [ _bannerView viewWithTag:999 ];
if( oldBanner )
{
[ oldBanner removeFromSuperview ];
oldBanner = nil;
}
banner.tag = 999;
[ _bannerView addSubview:banner ];
}

Related

Memory leak on Allocations

I'm a beginner and still trying to figure out how to read this.
There are 3 custom views and at the start I allocate the first one.
And then the second / deallocate the first and then the third / deallocate the second.
I do empty/nil all arrays right before deallocating each view so from what I see, all memory retained each time I allocated views, should be released whenever I deallocate/nil them but in the graph it keeps increasing, I don't see anything being released at all.
Is that supposed to look like that? I'm nil-ing delegates, arrays, dictionaries etc everything.
-(void)firstTOsecond {
[self.first removeFromSuperview];
self.first.delegate = nil;
self.first = nil;
self.second = [[Second alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.second];
self.second.delegate = self;}
-(void)secondTOthird {
[self.second removeFromSuperview];
self.second.delegate = nil;
self.second = nil;
self.third = [[Third alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[self.view addSubview:self.third];
self.third.delegate = self;}
EDIT
In First.m / Second.m / Third.m
-(instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.buttonStartFrame = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"buttonStartFrame"]];
[self addSubview:self.buttonStartFrame];
self.buttonStartFrame.userInteractionEnabled = YES;
self.buttonStartButton = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"buttonStartDefault"]];
[self.buttonStartFrame addSubview:self.buttonStartButton];
self.buttonStartButton.userInteractionEnabled = YES;
self.labelStart = [[UILabel alloc]init];
[self.buttonStartButton addSubview:self.labelStart];
self.labelStart.textColor = [UIColor darkGrayColor];
self.labelStart.text = #"Start";
}
return self;}
the memory leak could be that you are continuously creating new view elements when they could be updated instead .. you can create helper methods to avoid allocating new views, and updating the one's on the main thread instead
- (void)limitedUpdateTextColorView:(NSDictionary *)somethingCache {
self.labelStart.textColor = [somethingCache valueForKey:#"bar"];
self.labelStart.text = [somethingCache valueForKey:#"foo"];
}

Admob not showing in webViewDidFinishLoad when using iframe

I am creating an app using Phonegap and Onsen UI. I am able to show the Admob ads using this code:
- (void)webViewDidFinishLoad:(UIWebView*)theWebView
{
// Black base color for background matches the native apps
theWebView.backgroundColor = [UIColor blackColor];
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
return [super webViewDidFinishLoad:theWebView];
}
However, if I use iframe, the ads will not showing. Why this happened and how to solve it?

Hide Admob ads view in iPad

I put an Admob banner at the bottom centre of screen using the following code:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
However, in iPad, I plan to put different ads on another location. Therefore, I need to hide this ads in iPad. Is it possible for me to do this?
try this
bannerView_.hidden=true;
You do not need to make more than one banner view. The easier solution is using the same banner view but checking the current device idiom before positioning the view. For example here is the code you provided modified to check which device the user is on:
- (void) webViewDidFinishLoad: (UIWebView*) webView
{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
bannerView_.center = CGPointMake(self.view.center.x, self.view.frame.size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height/2);
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
// Change the banner's center/origin here for the iPad.
}
bannerView_.adUnitID = #"myid";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}

Zooming within UIWebView crashes app

I've looked at 20+ different posts that are vaguely related to this and haven't been able to find a solution.
What I have is a class which subclasses UIViewController. On this class's loadView method, I'm adding a UIWebView inside of a UIScrollView. The UIWebView has scalesPageToFit set to YES. The problem I'm facing is that it seems like zooms are being multiplied! i.e. When you zoom inside the web view the very first time, everything seems to work properly. When you zoom AGAIN, it seems that it takes your already zoomed version and tries to amplify that by an additional factor. This causes the app to crash. Any thoughts on how to fix this? I think something about the reported zoomScale from the UIWebView is messing things up.
Here's a modified sample code to get an idea of the current implementation:
# This is a UIViewController
#implementation MyViewController
- (void)loadView
{
[super loadView];
self.view.accessibilityLabel = #"MyView";
self.scrollView = [[UIScrollView alloc] init];
self.scrollView.backgroundColor = [UIColor blueColor];
self.scrollView.scrollsToTop = YES;
self.scrollView.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:self.scrollView];
// constraints removed from sample for readability
self.headerView = [[UIViewController alloc] init];
[self.scrollView addSubview:self.headerView];
self.bodyView = [[UIWebView alloc] init];
self.bodyView.delegate = self;
self.bodyView.scalesPageToFit = YES;
self.bodyView.scrollView.scrollsToTop = NO;
self.bodyView.translatesAutoresizingMaskIntoConstraints = NO;
[self.bodyView.scrollView addObserver:self
forKeyPath:KeyValuePath
options:NSKeyValueObservingOptionNew
context:nil];
[self.scrollView addSubview:self.bodyView];
// other constraints here
}
Any help or feedback would be immensely appreciated.

Integrating admob under a navigation bar

i have added all the required files and frameworks for integrating admob. The problem is that it does not add the subview to the viewcontroller.
My viewcontroller consist of a navigation bar and a UITableView. What i want to achieve is to have a admob in the button of my viewcontroller. How can i achieve this.
At the moment i'm using following code, which is not doing anything and not giving me any errors:
bannerView_.adUnitID = #"Banner Key";
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
Perhaps you need to set the frame of the banner as well?
1 must set the frame of ad banner so that it can be displayed:
m_bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
m_bannerView.adUnitID = #"AD Unit ID";
m_bannerView.rootViewController = self;
m_bannerView.delegate = self;
m_bannerView.frame = CGRectMake(0, 64, 320, 50); // 64 = height of navigation bar
GADRequest *request = [GADRequest request];
[self.view addSubview:m_bannerView];
[m_bannerView loadRequest:request];
You can set the origin points of the adBanner like below.
self.adBanner = [[GADBannerView alloc]initWithAdSize:kGADAdSizeBanner
origin:CGPointMake(0,31)];
self.adBanner.adUnitID = #"YOUR_KEY";
self.adBanner.delegate = self;
self.adBanner.rootViewController = self;
[self.view addSubview:self.adBanner];
[self.adBanner loadRequest:[self request]];

Resources