I have an app with a UITabBarController containing 5 tabs, where each tab is a UIViewController with a UITableView embedded in. I am bringing iAds and AdMobs to my app which will be removed using IAPs. This is a universal iPhone and iPad app.
At first, I implemented just the iAds with the use of Shared Banners and the AppDelegate and it worked really well. Now, before releasing, I am going to also bring AdMobs as a fallback incase any iAds don't load. I have set it up in the same way as the iAd.
Implementing the actual AdMob is not a problem in the same way, but I am facing issues when changing tabs.
Issue
If an iAd loads and I move from the first tab to the second tab, it continues to show that iAd. If an AdMob loads and I move from the first tab to the second tab, the AdMob disappears until it reloads again.
Here's some code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"VIEW WILL APPEAR");
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"View will appear and the IAP is not Successful");
[self displayiAdsOrNot];
}
else
{
NSLog(@"View will appear and the IAP IS Successful");
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = YES;
}
}
- (void)displayiAdsOrNot
{
NSLog(@"Display iAds or Not");
self.adMobBannerView.hidden = YES;
self.adBanner = [[self appdelegate] adBanners];
self.adBanner.delegate = self;
if (IDIOM == IPAD)
{
NSLog(@"***This is the iPad****");
[self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
[self.adBanner setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adBanner];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adBanner
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adBanner
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
else
{
NSLog(@"*** THIS IS THE IPHONE ***");
[self.adBanner setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-98, 320, 50)];
[self.view addSubview:self.adBanner];
}
}
Here are the delegate methods:
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(@"bannerViewDidLoadAd gets called");
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"bannerViewDidLoadAd gets called and the IAP is not successful, so we hide the AdMob and show the iAd");
self.adMobBannerView.hidden = YES;
self.adBanner.hidden = NO;
}
else
{
NSLog(@"bannerViewDidLoadAd gets called and the IAP IS Successful so we hide the AdMob and iAd");
self.adMobBannerView.hidden = YES;
self.adBanner.hidden = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"The didFailToReceiveAdWithError is called and it is unable to show ads in Timeline. Error: %@ %@", [error localizedDescription], [error domain]);
self.adBanner.hidden = true;
[self displayAdMobBannerOrNot];
}
- (void)displayAdMobBannerOrNot
{
NSLog(@"DisplayAdMobBannerOrNot is called");
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"IAPSuccessful"])
{
NSLog(@"The DisplayAdMonBannerOrNot is called and the IAP is not Successful");
self.adMobBannerView.hidden = NO;
self.adMobBannerView = [[self appdelegate] adMobBanners];
self.adMobBannerView.rootViewController = self;
self.adMobBannerView.delegate = self;
self.adMobBannerView.adUnitID = @"ca-app-pub-394025609333333333335716";
if (IDIOM == IPAD)
{
NSLog(@"The DisplayAdMobBannerOrNot is called and we are using an iPad");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-80, 320, 50)];
[self.adMobBannerView setTranslatesAutoresizingMaskIntoConstraints:NO];
[self.view addSubview:self.adMobBannerView];
NSLayoutConstraint *myConstraint =[NSLayoutConstraint
constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeTrailing
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTrailing
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
myConstraint =[NSLayoutConstraint constraintWithItem:self.adMobBannerView
attribute:NSLayoutAttributeBottom
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeBottom
multiplier:1
constant:0];
[self.view addConstraint:myConstraint];
}
else
{
NSLog(@"The DisplayAdMobBannerOrNot is called and we are using an iPhone");
[self.adMobBannerView setFrame:CGRectMake(0, [[UIScreen mainScreen] bounds].size.height-98, 414, 50)];
[self.view addSubview:self.adMobBannerView];
GADRequest *request = [GADRequest request];
request.testDevices = @[ @"151111111111ffb836f4d823ac" ];
[self.adMobBannerView loadRequest:request];
}
}
else
{
NSLog(@"The DisplayAdMobBannerOrNot is called and the IAP IS Successful so we'll just hide the iAd and the adMobBanner");
self.adBanner.hidden = YES;
self.adMobBannerView.hidden = YES;
}
}
So, when the viewWillAppear gets called (every time I come back to this UIViewController), I am checking if the IAPSuccessful bool is true. If it's not, I load up the displayiAdsOrNot method. If that fails, it's delegate will get called which calls the displayAdMobBannerOrNot.
Now, I completely understand why, when I have an AdMob displaying and I move from one view to another, it removes the AdMob because when I come back, the viewWillAppear loads up the sharedBanner for the iAd and not the AdMob.
With that in mind, I'm not really sure what I need to do. I want to ensure the AdMob is loading the sharedBanner every time it's loaded. So I put the sharedBanner code from the displayAdMobBannerOrNot into the displayiAdOrNot and it didn't change the behaviour, because it's not calling the functionality to actually place the ad (displayAdMobBannerOrNot).
As a test, in the viewWillAppear, when the IAPSuccessful is false, I called [self bannerView:self.adBanner didFailToReceiveAdWithError:nil]; instead of anything else and that worked. When I moved from tab one to two two, it kept the AdMob in display. However, that's of course not productional code. I'm just not able to see this clearly.
Any guidance on this would really be appreciated.
Aucun commentaire:
Enregistrer un commentaire