AdMob interstitial not showing What is wrong? - ios

I am trying to add admob interstitial ads to my app but when I run my app no ad shows up. I just get a message in the logs saying :
<Google> To get test ads on this device, call: request.testDevices = #[ #« deviceID » ];
I'm pretty sure it knows im trying to show an add because of a previous error but it just won't appear.
here is my code( I added a print statement to the first part but it doesn't seem to take it into considiration ):
if interstitialad != nil {
if interstitialad!.isReady {
interstitialad?.present(fromRootViewController: self)
}
}
here is the second part of the code ( note both are in ViewDidLoad )
if interstitialad != nil {
if interstitialad!.isReady {
interstitialad?.present(fromRootViewController: self)
}
}
func createAndLoadInterstitial() -> GADInterstitial {
let request = GADRequest()
let interstitial = GADInterstitial(adUnitID: "unit id")
//request.testDevices = ["iPhone test device ID"]
//request.testDevices = ["simulator ID"]
interstitial.delegate = self
interstitial.load(request)
return interstitial
}
interstitialad = createAndLoadInterstitial()
What is wrong with this?
Thanks !

Google Ad Interstitial Objective C Working code:
- (void)startAdInterstial
{
self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:FIREBASE_ADMOBID];;
GADRequest *request = [GADRequest request];
request.testDevices = #[kGADSimulatorID,#"7b5e0f6aad46c15f1e9ef2e6d3d381b7"];
_interstitial.delegate = self;
[self.interstitial loadRequest:request];
}
#pragma mark - interstitial delegate
- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
[_interstitial presentFromRootViewController:[APPDELEGATE window].rootViewController];
}
- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(#"error during full screen view %#",error);
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)interstitial {
}
Hope this will help you out in some other way.

Here are some code which I used to initiate the AdMob in Objective-c:
AdMobHandler.m file:
//created 320x60 banner and displayed at the bottom of UIView.
//If your app in development stage then you need to add the device ID or simulator ID which will appear during AdMob Initiation.
To get test ads on this device, call: request.testDevices = #[ #"xxxxxxxxxxxxxxxxxxxxxxxxxx" ];
- (void)initiateAndLoadAdMob:(UIViewController *)sender
{
GADRequest *request = [GADRequest request];
GADBannerView *bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
request.testDevices = #[ #"xxxxxxxxxxxxxxxxxxxxxxxxxx <<Device ID>>",#"xxxxxxx<<Simulator ID>>" ];
bannerView.adUnitID = FIREBASE_ADMOBID;
bannerView.rootViewController = (id)self;
bannerView.delegate = (id<GADBannerViewDelegate>)self;
senderView = sender.view;
bannerView.frame = CGRectMake(0, senderView.frame.size.height - bannerHeight, senderView.frame.size.width, bannerHeight);
[senderView addSubview:bannerView];
}
If you able to understand and change code to Swift will gives you some idea and solve your above issue. Let me know if you have any queries for above code.
For more details check this below link:
https://firebase.google.com/docs/admob/ios/interstitial

You said you're trying to present your GADInterstitial in your viewDidLoad. This is not when you should be presenting your GADInterstitial. You need to give the GADInterstitial time to load the ad. Presenting an ad in your viewDidLoad is also against the AdMob TOS. You should be presenting the GADInterstitial during a transition/action in your application. You should also implement the GADInterstitial delegate methods so you know why the ad is failing to load, when it has actually loaded, and when the user has dismissed the ad so you can continue to transition them in your application.
Disallowed interstitial implementations
GADInterstitial Ad Events

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.

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_];
}

iOS DoubleClick for Publishers (DFP) .. Error : No ad to show

I am using DoubleClick for Publishers (DFP) in my App. I have integrated it as per DOCs as follows.
dfpBannerView_ = [[DFPBannerView alloc] initWithAdSize:kGADAdSizeBanner];
dfpBannerView_.adUnitID = kSampleAdUnitID;
// Set the delegate to listen for GADBannerViewDelegate events.
dfpBannerView_.delegate = self;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
dfpBannerView_.rootViewController = self;
[self.view addSubview:dfpBannerView_];
// Initiate a generic request to load it with an ad.
[dfpBannerView_ loadRequest:[GADRequest request]];
where kSampleAdUnitID is the App Unit Id I have created.
following Delegate Method is getcalled
- (void)adView:(DFPBannerView *)view
didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(#"Failed to receive ad with error: %#", [error localizedFailureReason]);
NSLog(#"%d",error.code);
}
But still I gets error as follows,
Failed to receive ad with error: Request Error: No ad to show.
2014-04-11 17:40:55.836 DFPBannerExample[6319:70b] 1
Where error Code 1 stands for /// The ad request was successful, but no ad was returned.
kGADErrorNoFill,
I am not getting where is the problem. Why I am not getting Ads ,while I am getting Ads for Sample App Unit Id .
if you want your banner to display something, you need to specify the banner size, and then tag the banner with the adunit id: check this code:
bannerView_ = [[DFPBannerView alloc] initWithFrame:CGRectMake(111, 483, 537, 120)];
bannerView_.adUnitID = #"/5900406/UserAppWaitingView";
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:[GADRequest request]];

google admob ios: Request Error: No ad to show

I found a related thread AdMob Ios Error: Failed to receive ad with error: Request Error: No ad to show but it does not solve my problem.
I am trying to create an Interstitial ad using the latest example project from google code: https://google-mobile-dev.googlecode.com/files/InterstitialExample_iOS_3.0.zip.
I have changed kSampleAdUnitID to that of my ad id.
Yesterday, when I clicked on Load Interstitial, I got Request Error: No ad to show. I had to click 4-5 times for it to work.
Today, I am using the same code unchanged but no matter how many times I click on Load Interstitial, I get the error mentioned above.
Any help here?
My implementation as follows - I hope it helps:
#interface ViewController () <GADInterstitialDelegate>
#property (strong, nonatomic) GADInterstitial *interstitial;
#end
#implementation ViewController
#define MY_INTERSTITIAL_UNIT_ID #"...."
- (void)preLoadInterstitial {
//Call this method as soon as you can - loadRequest will run in the background and your interstitial will be ready when you need to show it
GADRequest *request = [GADRequest request];
self.interstitial = [[GADInterstitial alloc] init];
self.interstitial.delegate = self;
self.interstitial.adUnitID = MY_INTERSTITIAL_UNIT_ID;
[self.interstitial loadRequest:request];
}
- (void)interstitialDidDismissScreen:(GADInterstitial *)ad
{
//An interstitial object can only be used once - so it's useful to automatically load a new one when the current one is dismissed
[self preLoadInterstitial];
}
- (void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error
{
//If an error occurs and the interstitial is not received you might want to retry automatically after a certain interval
[NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:#selector(preLoadInterstitial) userInfo:nil repeats:NO];
}
- (void) showInterstitial
{
//Call this method when you want to show the interstitial - the method should double check that the interstitial has not been used before trying to present it
if (!self.interstitial.hasBeenUsed) [self.interstitial presentFromRootViewController:self];
}
#end

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]);}

Resources