I am implementing iAds into my app for the first time and the app itself is a simple 5-tabbed UITabBarController
where the first 4 tabs are a UIViewController
with a UITableView
embedded in and the last tab is a UITableViewController
.
With following this guide: http://www.appcoda.com/ios-iad-introduction/ I have successfully implemented the iAds and it shows up in the app and works it's way through each delegate method.
I have made it so that the first 4 tabs have the iAds, but the last tab does not. This is because the last tab is a static UITableView
and I cannot embed that into a UIViewController
, etc. So the first 4 tabs have the adBannerView
. The AdBannerView
is added in from Storyboard
into this UIViewController
.
Issue
During developing on a real device, running iOS 8.1.1 (and on another device running 8.2), when I am on the first tab, it shows the iAd Banner with no issues. When I switch to the second tab and come back to the first tab, two things happen:
1) The adBannerView
is there, but the actual ad itself is shown for a second and then disappears again.
2) The delegate method below outputs an error as soon as I leave the first tab:
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"Unable to show ads. Error: %@", [error localizedDescription]);
}
NSLog: Unable to show ads. Error: The operation couldn’t be completed. Ad was unloaded from this banner.
A few things to point out here. If I move to the 5th tab which itself doesn't contain the adBannerView
and come back to the first tab, this issue doesn't occur and the adBannerView
itself it still shown with the ads.
Also, with the use of NSLogs
, I can see that when I come back to the first tab from the second tab, viewWillAppear
gets run, which is immediately followed by:
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
This indicates the ad is ready to be loaded, but it takes a good 2 minutes for the ad to actually be displayed in the adBannerView
. When I come back to the first tab from the second tab, the adBannerView
is there, but the ads are not.
Here's some code:
- (void)bannerViewWillLoadAd:(ADBannerView *)banner
{
NSLog(@"AD Banner will load ad");
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
NSLog(@"Ad Banner did load ad");
// We must show the ad banner view when an ad is ready to be displayed.
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 1.0;
}];
// self.adBanner.hidden = NO;
}
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
NSLog(@"Ad Banner action is about to begin");
return YES;
}
- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
NSLog(@"Ad Banner action did finish");
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(@"Unable to show ads. Error: %@", [error localizedDescription]);
// When there are no ads to show, we should hide it:
Hide the ad banner.
[UIView animateWithDuration:0.5 animations:^{
self.adBanner.alpha = 0.0;
}];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.adBanner.delegate = self;
self.adBanner.alpha = 0.0;
}
I have read the Apple guide (https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/iAd_Guide/TestingiAdApplications/TestingiAdApplications.html) which states that occasionally, errors will be sent to the app to test that out as well and that's fine, but in this case, it seems like the app itself is unloading the banner view but then taking a long time to load it again.
Also, with looking at some similar situations on Stack Overflow, I have even tried to set the adBannerView delegate to nil in the viewWillDisappear
. When I do this, and I change from the first tab to the second tab, I don't get the error saying the ad has been unloaded in the delegate method and when I come back, it shows the AdBannerView
but without the ads.
- (void)viewWillDisappear:(BOOL)animated
{
NSLog(@"DISAPEARING");
self.adBanner.delegate = nil;
}
So to confirm, the issue is that when I leave the first tab to go to the second tab, the NSLog
outputs an error saying the Ad has been unloaded. When I come back to the first tab, it runs through the delegate methods and even goes to run the method that indicates the ad is ready to be shown, the adBannerView
is visible, but the ad itself is not, for about 2 minutes.
Update
I have implemented the use of shared banners using the AppDelegate
and the code in this example: https://www.youtube.com/watch?v=_0Mv44FWw0A&feature=youtu.be.
The progress is that I'm now not receiving any errors when switching tabs, but when I switch to the second tab and come back again, it still shows the banner but loads the ad into it after 2 minutes, even though the bannerViewDidLoadAd
delegate method IS being called as soon as I return back to the first tab.
Any guidance on this would really be appreciated.