Unable to hide admob banner programatically in iOS - ios

I seem to be unable to hide an Admob banner during my game, using the following method to show;
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
bannerView_.delegate=self;
bannerView_.adUnitID = ADMOB_ID;
bannerView_.rootViewController = self.viewController;
[viewController.view addSubview:bannerView_];
GADRequest *request =[GADRequest request];
[bannerView_ loadRequest:request];
When trying to hide it, even to test it I have hooked it up to a button
- (void)removeBanner{
NSLog(#"should be removing banner");
[bannerView_ setHidden:YES];
[bannerView_ removeFromSuperview];
}
I have tried both of the above options inside removeBanner, however none of them make the banner disappear?

A couple of things to check
Do you call the first function more than once? Put an assert that bannerView_ is not already set. If it were called more than once, you'd only be removing the last one.
Is bannerView_ somehow nil in removeBanner?
edit: probably you should set bannerView_ to nil after you remove it (but I doubt that would matter). Also, you could call setNeedsDisplay on viewController.view (and then displayIfNeeded to force it to happen immediately)

Related

GADRequest request delegate does not always receive resposne

I am working on an iOS app that uses Google Ad SDK. The view controller that is supposed to display the ad is a delegate of GADBannerViewDelegate and GADSwipeableBannerViewDelegate.
The ad comes in just fine most of the time. Sometimes however, AdMob seems to hang and I don't get a response whatsoever in my delegate.
Is there something I have done wrong? Alternatively, is there a way to detect this behavior?
I initialize the ad banner and then in viewWillAppear I reload the ad (assuming it's not already loading). It usually works just fine but again, it sometimes doesn't.
Here is the relevant code:
UPDATE::
I changed the code but to no avail. Here is new code (got rid of previous two methods):
-(void)resetAdView:(UIViewController *)rootViewController {
[self.adMob_ActivityIndicator startAnimating];
if (adBanner_ == nil) {
adBanner_ = [[DFPSwipeableBannerView alloc]
initWithFrame:CGRectMake(30, 365, 300, 150)];
}
[adBanner_ setHidden:YES];
if (isLoaded_) {
GADRequest *request = [GADRequest request];
[adBanner_ loadRequest:request];
[self.scrollView addSubview:adBanner_];
} else {
adBanner_.delegate = self;
adBanner_.rootViewController = rootViewController;
adBanner_.adUnitID = AD_UNIT_ID;
GADRequest *request = [GADRequest request];
[adBanner_ loadRequest:request];
[self.scrollView addSubview:adBanner_];
}
}
My instance variables are :
DFPSwipeableBannerView *adBanner_;
BOOL isLoaded_;
This might be due to lack of ad inventory.
Did you implement the banner view's error catching delegate function?
(void)adView:(DFPBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error
If this delegate method isn't implemented in your code, implement it and log the error object to get a detailed error description.
I ended up just wiping the banner view from the superview and completely re-initializing it and the request. This doesn't solve the problem of the hanging response but now if you switch to a different view and come back to this one, the ad does reload. It's possible that I was simply adding too many views into my scrollview which will not be the case anymore.
-(void)resetAdView:(UIViewController *)rootViewController {
// in reset method
[self.adMob_ActivityIndicator startAnimating];
adBanner_ = nil;
for (UIView *subview in self.scrollView.subviews) {
if (subview.tag == 666) {
// removing banner view from superview
[subview removeFromSuperview];
}
}
// (re) initializing banner view
adBanner_ = [[DFPSwipeableBannerView alloc]
initWithFrame:CGRectMake(30, 365, 300, 150)];
[adBanner_ setHidden:YES];
// setting up and requesting ad
adBanner_.delegate = self;
adBanner_.rootViewController = rootViewController;
adBanner_.adUnitID = AD_UNIT_ID;
GADRequest *request = [GADRequest request];
[adBanner_ loadRequest:request];
adBanner_.tag = 666;
[self.scrollView addSubview:adBanner_];
}

Can't remove Admob Banner from the view (iOS)

I have implemented Admob into my app but i've noticed that if the Admob view doesn't receive an ad, I can't remove it from the superview. If it already has an ad loaded it just stays there with that ad loaded even if the device is not connected to the internet. This is my code:
self.admobBannerView = [[GADBannerView alloc] init];
self.admobBannerView.frame = CGRectMake(0.0,self.view.frame.size.height-50,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height);
self.admobBannerView.adUnitID = #"...";
self.admobBannerView.rootViewController = self;
self.admobBannerView.delegate = self;
[self.view addSubview:self.admobBannerView];
[self.admobBannerView loadRequest:[GADRequest request]];
Then the Admob delegate
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
[self.admobBannerView removeFromSuperview];
}
Despite calling [self.admobBannerView removeFromSuperview]; the banner remains where it is. I can't understand why this is happening. Any help is appreciated.
Thanks
I had the same problem and debugged the view hierarchy w/ Xcode only to find multiple GADBannerViews existed. Fixed the code to check if ad view already existed before adding one.
In your case you should wrap the ad view creation in:
if (self.admobBannerView != nil)
{
// create ad
}

Admob 6.8.0: manually remove, hide or disable banners

There are tons of similar questions here, but none of the solutions are working on the latest AdMob SDK. At least I couldn't make it work.
Loading:
- (void)AdMob_Banner_On
{
bannerView_.adUnitID = kAdMobID;
bannerView_.rootViewController = self;
[bannerView_ loadRequest:[GADRequest request]];
(...)
}
They are running perfectly, and now removing... all solutions I found are quite simple, but they don't work:
- (void)AdMob_Banner_Off {
NSLog(#"Admob: Turning Off");
bannerView_.hidden = YES;
[bannerView_ removeFromSuperview];
[bannerView_ setDelegate:nil];
bannerView_ = nil;
}
Some times I need all screen available, I can't show ads all the time. Any ideas?
In case anyone has the same problem, here is how I fix it.
The problem was that I was that the lines on the "AdMob_Banner_On" method were inverted.
Although the banner was showing up perfectly, it made the bannerView_ unable to respond to any other command, so even if the AdMob_Banner_Off was called, nothing was happening.
Now the code that works. The first thing you should do it to set the position of the banner, and then calling it. That was my problem. This code should work fine:
Turning on:
- (void)AdMob_Banner_On {
NSLog(#"Admob: Turning On");
// Making it on the bottom:
CGPoint origin = CGPointMake(0.0,self.view.frame.size.height - CGSizeFromGADAdSize(kGADAdSizeSmartBannerPortrait).height);
bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait origin:origin];
bannerView_.adUnitID = kAdMobID;
bannerView_.rootViewController = self;
bannerView_.delegate = self;
[self.view addSubview:bannerView_];
[bannerView_ loadRequest:[GADRequest request]];
}
- (void)AdMob_Banner_Off: {
NSLog(#"Admob: Turning Off");
[bannerView_ removeFromSuperview];
}
With this code you can turn on and off the banner as you want. This is useful to apps that can't show the banner all the time.
initAd must be in viewDidLoad
I solved by it.

Admob Banner doesn't show

I want to implement Admob into an app for iPad in landscape mode, I followed step by step tutorial for admob, on adding advertising to the app. I added all the frameworks and requirements given to work correctly.
I can't see the banner. I tried both the simulator and on the device itself. According to the following link it seems that:
"Test ads are not supported on iOS 6 devices; use the simulator instead"
The compilation is correct without issues.
I changed the background of the red banner, and I moved all the views that would hide the banner, but still I couldn't see it. Inside the viewDidLoad method, I have placed it in different positions
From Google admob statistics I see that increase requests, this I don't understand. O___O
The AdMob SDK is the latest.
I appreciate any help.
My code:
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
GAD_SIZE_728x90.height,
GAD_SIZE_728x90.width,
GAD_SIZE_728x90.height)];
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
bannerView_.delegate = self;
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
[self.view bringSubviewToFront:bannerView_];
bannerView_.backgroundColor= [UIColor redColor];
GADRequest *request = [GADRequest request];
request.testDevices = [NSArray arrayWithObjects:
#"6A1E19F-xxxxx-xxxx-xxxxDF38593ECBD" ,
#"9bc75xxxxxxxxxxxxxxx762de7c79fb",
nil];
request.testing = YES;
[bannerView_ loadRequest:request];
try this
request.testDevices = [NSArray arrayWithObjects:#[#"GAD_SIMULATOR_ID"], nil];
[yourbannerview loadRequest:request];
and delete request.test, is disabled now :/

self.addSubView not working on iPad

I have an app that attempts to retrieve an iAd and if is unsuccessful loads an AdMob ad. It works perfectly on iPhone but when run on iPad I get a continuous loop that prevents the app from loading. Here's some code:
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
#try {
NSLog(#"Ad Error, looking for AdMob Ad...");
// Create a view of the standard size at the bottom of the screen.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
GAD_SIZE_320x50.height,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
// I know the banner size is incorret for iPad but it's only supposed to run
// on iPad in compatibility mode and changing doesn't help
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = #"XXXXXXX";
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
GADRequest *admobRequest = [GADRequest request];
admobRequest.testDevices = [NSArray arrayWithObjects:
GAD_SIMULATOR_ID,
nil];
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest: admobRequest];
[iAdBanner setHidden: YES];
[bannerView_ setHidden: NO];
}
#catch (NSException *e) {
NSLog(#"Exception: %#", e);
}
#finally {
}
}
The NSLog prints out continuously and the app doesn't load.
012-02-24 21:58:38.991 TrophyConverter Free[2948:15e03] Ad Error, looking for AdMob Ad...
2012-02-24 21:58:38.992 TrophyConverter Free[2948:15e03] Ad Error, looking for AdMob Ad...
2012-02-24 21:58:38.994 TrophyConverter Free[2948:15e03] Ad Error, looking for AdMob Ad...
Has anyone else experienced this? How do I over come it? I've managed to use a bool to stop the continuous log printing and load the app but no Ad is shown.
EDIT:
The issue completely disappears when I remove this line
[self.view addSubview:bannerView_];
which isn't helpful since this is the part that adds the AdMob view.
I've also tried changing the build target to be a universal app rather than a iPhone app. This fixes the problem but I don't want it to be a universal app and I have no layouts created for it.
I couldn't find a fix for this in the end so I basically added a bool that was switched once one fail had been made. Then no ad would be added if the check had been done and not already worked.

Resources