AdMob ad test banner NEVER load in mediation - ios

-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
self.iAdbanner.hidden=YES;
_bannerView=[[GADBannerView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
self.bannerView.adUnitID=#"UNIT_ID";
self.bannerView.rootViewController=self;
[self.view addSubview:self.bannerView];
self.bannerView.delegate = self;
_bannerView.hidden=NO;
GADRequest *request =[GADRequest request];
request.testDevices= #[ GAD_SIMULATOR_ID ];
[self.bannerView loadRequest:request];
}
Only the iAd test banner loads. However, when the iAd test banner fails, I don't understand why AdMob test banner never loads in simulator and device. Didn't I implement the correct code for AdMob test banner to fill the spot of iAd banner failures?

You're better off creating a Singleton class Ad banner to share a single ad on multiple view controllers.

Are you setting the size you are requesting? Something like:
self.bannerView.adSize = kGADAdSizeSmartBannerPortrait;

Related

GADBannerView delegate methods not called if the view is not in the view hierarchy

I'm working with the Google Mobile Ads SDK on iOS and trying to display some ads. My code:
GADBannerView* bannerView = [[GADBannerView alloc] initWithAdSize:GADAdSizeFromCGSize(CGSizeMake(300, 250))];
bannerView.adUnitID = #"hidden";
bannerView.rootViewController = self;
bannerView.delegate = self;
GADRequest* request = [GADRequest request];
request.testDevices = #[ kGADSimulatorID ];
[bannerView loadRequest:request];
This works fine if I add the bannerView to the view hierarchy right after the code you see above. However, I don't really want to add it until the ad is loaded, so I wanted to delay it. I noticed that if the bannerView is not in the view hierarchy, the delegate methods are not called at all. Furthermore, I have found this answer, which is in line with what I'm observing. On the other hand, this is a quote from the GADBannerViewDelegate header:
/// Tells the delegate that an ad request successfully received an ad. The delegate may want to add
/// the banner view to the view hierarchy if it hasn't been added yet.
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView;
This suggests that it should be possible to receive those delegate callbacks even if the view is not in the hierarchy, which is exactly what I want. So, any ideas how could I achieve this?
Ok, so the problem here was that I didn't keep the reference to the bannerView. It was deallocated after the method returned, and this is why the delegate methods were not called.
I just had the same issue after upgrading from the Admob SDK 7.56 to 8.2:
They changed the method names of the GADBannerViewDelegate protocol.
E.g. instead of
-(void)adViewDidReceiveAd:(GADBannerView *)adView;
it is now
-(void)bannerViewDidReceiveAd:(GADBannerView *)bannerView;
see also the migration guide to Admob SDK version 8:
https://developers.google.com/admob/ios/migration#methods_removedreplaced
You should add the GADBannerView to your view and set its hidden property to YES initially. Also, I'd suggest using the AdSize Constant kGADAdSizeBanner that AdMob provides. Here's a list of additional AdSize Constants.
For example:
bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
bannerView.adUnitID = #"YourAdUnitID";
bannerView.rootViewController = self;
bannerView.delegate = self;
[bannerView loadRequest:[GADRequest request]];
bannerView.hidden = YES; // Hide banner initially
[self.view addSubview:bannerView];
// This will put the banner at the bottom of the screen and stretch to fit the screens width
[bannerView setFrame:CGRectMake(0, self.view.frame.size.height - bannerView.frame.size.height, self.view.frame.size.width, bannerView.frame.size.height)];
Then, when you receive an ad you unhide the banner. For example:
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
// We've received an ad so lets show the banner
bannerView.hidden = NO;
NSLog(#"adViewDidReceiveAd");
}
-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
// Failed to receive an ad from AdMob so lets hide the banner
bannerView.hidden = YES;
NSLog(#"adView:didFailToReceiveAdWithError: %#", [error localizedDescription]);
}
You could also animate this, if you'd prefer, by setting the banner's alpha property to 0.0 initially instead of using it's hidden property. Then, animate the alpha when you receive an ad. For example:
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
// We've received an ad so lets fade in the banner
[UIView animateWithDuration:0.2 animations:^{
bannerView.alpha = 1.0;
}];
NSLog(#"adViewDidReceiveAd");
}
-(void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
// Failed to receive an ad from AdMob so lets fade out the banner
[UIView animateWithDuration:0.2 animations:^{
bannerView.alpha = 0.0;
}];
NSLog(#"adView:didFailToReceiveAdWithError: %#", [error localizedDescription]);
}
Also, as a side note, the GADBannerView is transparent when there is no ad to display. So, adding it to your view and doing nothing else would work too.

Unable to hide admob banner programatically in 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)

iOS App does not resume after clicking AdMob ad

I use class to show AdMob ad and it has 320x50 size view, i call only this class's view where i want to display my ad in app but when i click ad and want to return back, it go back to AdMob's class and it resizes to cover all screen (Autoresize is closed) . Does anybody has solution about it?
I use this AdMob class with small view because of my other m files are mm. file and i could not make AdMob work in mm files and its also easy to call just this class's view when i need to display ad.
I use same codes as in AdMob example in official site.
-(void)setAdMob{
CGPoint origin = CGPointMake(0.0,
self.view.frame.size.height -
CGSizeFromGADAdSize(kGADAdSizeBanner).height);
// Use predefined GADAdSize constants to define the GADBannerView.
self.adBanner = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner
origin:origin];
// Note: Edit SampleConstants.h to provide a definition for kSampleAdUnitID
// before compiling.
self.adBanner.adUnitID = kSampleAdUnitID;
self.adBanner.delegate = self;
[self.adBanner setRootViewController:self];
[self.view addSubview:self.adBanner];
self.adBanner.center =
CGPointMake(self.view.center.x, self.adBanner.center.y);
self.adBanner.frame=CGRectMake(0, 0, self.adBanner.frame.size.width, self.adBanner.frame.size.height);
[self.adBanner loadRequest:[self createRequest]];
}
-(NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskPortrait;
}
-(GADRequest *)createRequest {
GADRequest *request = [GADRequest request];
request.testDevices =
[NSArray arrayWithObjects:
GAD_SIMULATOR_ID,
// TODO: Add your device/simulator test identifiers here. They are
// printed to the console when the app is launched.
nil];
return request;
}
// We've received an ad successfully.
-(void)adViewDidReceiveAd:(GADBannerView *)adView {
NSLog(#"Received ad successfully");}
- (void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(#"Failed to receive ad with error: %#", [error localizedFailureReason]);}

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