I have a UITableViewController that I want to add iAds to. I want the ad to display at the bottom of the screen at all times. I have followed the answer here: https://stackoverflow.com/a/9857798/2584268 and have achieved this behavior. However, the ad is hidden in the beginning and only appears when the user scrolls the table. How can I get the ad to appear at the bottom of the screen when the view loads, not just when the user scrolls?
Asked
Active
Viewed 170 times
1
-
Do you still need an answer for this? – Douglas Jul 31 '14 at 22:48
-
@Douglas I believe I figured it out. All I did was set self.candisplaybannerads to YES and it automatically puts it in. If you had another answer that worked I would love to hear it though. – Jacob Jul 31 '14 at 22:51
-
That works, but leads to problems if you have more than one view controller displaying iAds. I made mine with code and then placed the iAd on viewDidLoad and also set up a notification to listen for rotation. Let me know if you want me to post the code as an answer. – Douglas Jul 31 '14 at 22:56
-
@Douglas could you explain the problems with my solution? Also, I would like to see yours as well! – Jacob Jul 31 '14 at 22:57
-
The only problem is if you have multiple view controllers. Sometimes the ads will appear on your simulator and test devices but there is a warning error code 7 or something like that where the ad is unloaded. Ads like this don't appear on approved apps. If you only have one view controller, what you did was fine. – Douglas Jul 31 '14 at 23:00
-
@Douglas Oh, I never knew that. I was told to do this on the Apple Dev Forums and they said there is no error handling needed. So if I have that line on more than one of my controllers an ad won't show on all of them? – Jacob Jul 31 '14 at 23:05
-
@Douglas I have never had an ad not display on my test devices or simulator with this line of code, so I don't really understand the error. – Jacob Jul 31 '14 at 23:09
-
Just see if you get an error in the console. I have one app that I did exactly what you did on two of my view controllers and I missed the error and had no ads showing up, but they appeared in testing. – Douglas Jul 31 '14 at 23:10
-
The error is ADErrorDomain Code=7. This ad was unloaded. I kept getting that but didn't see it in my console because I had other things I was logging. So all looked fine in testing, but not once I submitted the app. – Douglas Jul 31 '14 at 23:13
-
@Douglas did it have something to do with your ad fill rate settings for testing? I am checking my console now and I don't see the error. The ads seem to be displaying fine. – Jacob Jul 31 '14 at 23:16
-
No, I have it set to fill 50%, but when it loaded I got the error that I didn't see. But if you are not getting the error, everything must be fine. – Douglas Jul 31 '14 at 23:18
-
I have mine set to 100, but even when I changed it to 20% I didn't get that error. I will try out your answer too... – Jacob Jul 31 '14 at 23:21
-
Cool, I posted on my blog the steps I used to make the adbanner in the app delegate if you want to take a look. http://thefizixgroup.blogspot.com/p/iad-implementation_6.html Good luck. You probably won't run into any problems with the self.candisplay... but I didn't know you had used that until you answered my question, do you still need an answer? – Douglas Jul 31 '14 at 23:29
-
Thanks. I'll take a look at it. I appreciate the answer, and if it works for me I'll mark as correct! – Jacob Jul 31 '14 at 23:30
1 Answers
1
To show the ad when the view appears, I add the banner view on viewDidAppear.
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_myIAD = [[self appdelegate] myIAD];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
//iPhone code
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 50, 320, 50);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 114, 320, 50);
}
}
else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 32, 480, 32);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 84, 480, 32);
}
}
}
else
{
//iPad code
if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortraitUpsideDown)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 768, 66);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 768, 66);
}
}
else if ([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight)
{
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_6_1)
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 66, 1024, 66);
}
else
{
_myIAD.frame = CGRectMake(0, _myTableView.frame.size.height - 130, 1024, 66);
}
}
}
[self.view addSubview:_myIAD];
}
There is a ton of code in there to deal with iOS6.1 and iPads and iPhones, but it works real well. I always like to use the statusBarOrientation to find the orientation, it works better.
As you can see there is a line
_myIAD = [[self appdelegate] myIAD];
I actually make the banner in the app delegate and use it in all the view controllers. To handle rotation, I register for a notification in viewDidLoad.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateUI:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
And basically use the same code in the viewDidAppear in the method updateUI:
The only other thing I did was to put in this code to keep the iAd at the bottom.
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
CGRect frame = _myIAD.frame;
frame.origin.y = _myTableView.contentOffset.y + _myTableView.frame.size.height-_myIAD.frame.size.height; // Move view to the bottom on scroll
_myIAD.frame = frame;
[_myTableView bringSubviewToFront:_myIAD];
}

Douglas
- 2,524
- 3
- 29
- 44
-
QuestionL: If I am using a tab controller will I be able to use this same ad in all of my view controllers managed by the tab controller? Ie, a new ad with new content won't have to appear, and it will be as if they were looking at the same ad the whole time... – Jacob Jul 31 '14 at 23:45
-
Yes, that is the job of the app delegate, to hold the adbanner and then just have each view controller show that ad banner. It works seamlessly. The ad on one vc is then continued on the next vc. Although my code works with a navigation controller so you would have to adjust the layout of the iAd frame to make it fit for the tab bar controller. – Douglas Jul 31 '14 at 23:58
-
Wow, I must say, very stellar blog post and answer here, I really appreciate it! Thanks for all your help man. I will try to make it work with the tab controller. Thanks again! – Jacob Aug 01 '14 at 00:10
-
Not a problem, I think you will just have to adjust the position of the iAd. In my tab controller I put the iAd at top so it made it pretty easy. – Douglas Aug 01 '14 at 12:11
-
@Jacob, just wanted to let you know that my app that I used this iAd implementation in, just got approved and is now seeing live ads. So it works! Just wanted to let you know. – Douglas Aug 08 '14 at 19:34