iAd in iPhone animates when not loaded - iphone

I have added a banner view for implementing iAd in the bottom of the screen. But when running on the simulator the banner view is slightly above the frame fixed for it. It appears as a transparent strip and is not selectable. After sometime it automatically comes to the bottom as a black strip saying Test Advertisement.
I want the banner View to stick to the bottom and not animate.
Here is my code. The adView declaration code:
adView = [[[ADBannerView alloc] initWithFrame:CGRectOffset(CGRectZero, 0, 350)] autorelease];
adView.frame = CGRectMake(0,340,320,25);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifier320x50];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
adView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleRightMargin;
adView.tag = 111;
[self.navigationController.view addSubview:adView];
adView.delegate = self;
self.bannerIsVisible = NO;
adView.hidden = YES;
Here are the delegate methods.
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is invisible now and moved out of the screen on 50 px
banner.frame = CGRectOffset(banner.frame, 0, 50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
// When iAd is not availale on the banner
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// banner is visible and we move it out of the screen, due to connection issue
banner.frame = CGRectOffset(banner.frame, 0, 520);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
I am not able to understand the problem. Kindly help.

First.
When the main view is loaded how are the objects placed in it initially? I always place them in right positions inside viewDidLoad method. So place your banner below the bottom of the main view making sure that
myBannerFrame.origin.x = 0;
myBannerFrame.origin.y = mainView.frame.size.height;
[myBanner setFrame:myBannerFrame]; // no need for animation here!
Also viewDidLoad is a good place to set your flag self.bannerIsVisible to FALSE.
Second. Do not use fixed pixel values like here
banner.frame = CGRectOffset(banner.frame, 0, 50);
instead of 50 you better put banner.frame.size.height
This will help you to avoid many errors and misalignments.

Related

Cannot seem to move iAd banner to the top of the screen with this code

I have searched and searched and have done lots of trial and error on this code trying to move the ad banner to the top of the screen. It has been a week already with no progress. If anyone could help me out with this issue I would be thrilled. The code this is based off of is by the gentleman's great tutorial:
http://codewithchris.com/iad-tutorial/
Here's the code:
#interface ViewController ()
{
BOOL _bannerIsVisible;
ADBannerView *_adBanner;
}
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
_adBanner.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_bannerIsVisible)
{
// If banner isn't part of view hierarchy, add it
if (_adBanner.superview == nil)
{
[self.view addSubview:_adBanner];
}
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
NSLog(#"Failed to retrieve ad");
if (_bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
_bannerIsVisible = NO;
}
}
#end
Change this line:
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height, 320, 50)];
To this:
_adBanner = [[ADBannerView alloc] initWithFrame:CGRectMake(0, -50, 320, 50)];
(where the -50 is the height of the ad banner.) Before you were setting the ad banner's frame to the bottom of the screen using self.view.frame.size.height, whereas now the -50 moves the frame just above the screen.
Also, don't forget you'll need to reverse your CGRectOffsets' y coordinates (remove the (-) from the first and add it to the second) because the ones you have at the moment assume the frame is at the bottom of the screen.
Hope this helps!

App rejected because of hidden whenever ad content is not being served by iAd

My app just got rejected because "The banner within the app should be hidden whenever ad content is not being served by iAd." Then they supply this sample code;
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// assumes the banner view is at the top of the screen.
banner.frame = CGRectOffset(banner.frame, 0, -banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
Now my iAd displays at the bottom of the screen not the top. Also I wanted to take into count 3.5 vs 4 inch screens so here is my code;
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
CGSize result = [[UIScreen mainScreen] bounds].size;
CGFloat scale = [UIScreen mainScreen].scale;
result = CGSizeMake(result.width * scale, result.height * scale);
if(result.height == 1136){
banner.frame = CGRectOffset(banner.frame, 498, -banner.frame.size.height);
}else{
banner.frame = CGRectOffset(banner.frame, 410, -banner.frame.size.height);
}
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
The really annoying part is that my codes works correctly on my test iPhone and in the iOS simulator.
What am I doing wrong?
If your banner is in the bottom, you should sum banner.frame.size.height instead of substract to hide the banner. There is no need to do extra calculations for different screens.
So, this should be enough:
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);

iAd Banner not working

SO I used a tutorial to create an iAd Banner at the bottom of the screen and animate it into and out of the window, however the app is a tab based, and i do not quite know the correct offset, so you could tell me I would appreciate it, however the primary problem, is that the adBanner does not always appear, and when it does sometimes it is just a white box. Here is my code.
In my .h
#interface section3 <ADBannerViewDelegate>{
ADBannerView *adView;
BOOL bannerIsVisible;
}
#property (nonatomic, assign) BOOL bannerIsVisible;
//in the .m in the view did load
adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0.0, 410.0f);
adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
[self.view addSubview:adView];
adView.delegate = self;
self.bannerIsVisible = NO;
-(void)bannerViewDidLoadAd:(ADBannerView *)banner{
if (!self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, -50.0f);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error{
if (self.bannerIsVisible) {
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame, 0.0, 50.0f);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
I am using Xcode 4.2.1
Thank you very much, any help would be appreciated :)
The tabbar is 44px high so u must add to one of the "50" 44 and make 410 to the full screen size of 3.5 inch means to 480 default. That should be the solution.

xcode 4.3 - storyboard - iAd keeps moving

I have added iAd to my iphone app to be at the top of my app. originally I place it at x=0 and y=-50 so that it comes from off the screen. I use the following code for it in my .m :
- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
// Assumes the banner view is placed at the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)abanner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
// Assumes the banner view is just off the bottom of the screen.
banner.frame = CGRectOffset(banner.frame, 0, banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
When my app launch iAd get displayed at the top without any problem. but when I open another app and come back to it (without killing it so my app is running in the background) the banner moves another 50 pixel down
any idea?
You are adding 50.0px to banner.frame.origin.y in both cases.
Anyway: even if you'd be substracting 50.px in didFailToReceiveAdWithError: it
could happen that didFailToReceiveAdWithError: would get called multiple times in a
row and your code could move the banner higher and higher up (-50.0,-100.0,-150.0...).
So it's better to hardcode the hidden & visible positions instead of calculating it.
Try this:
- (void)bannerView:(ADBannerView *)abanner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
banner.frame = CGRectMake(0.0,-50.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)bannerViewDidLoadAd:(ADBannerView *)abanner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
banner.frame = CGRectMake(0.0,0.0,banner.frame.size.width,banner.frame.size.height);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}

how to add a custom subview inside TTThumbviewController (Three20)

i am developing an iphone application which have to show images from server as thumbnail list. I have created thumbnailview using Three20 package TTThumbViewcontroller class.
Now i have to add banner view above the thumbnail view as shows in the image. Also i have to add the bottom banner view in the TTPhotoviewcontroller also.
can anyone guide me, how to add my custom banner view (UIView) along with either TTThumbviewConntroller or TTPhotoViewController to my parent view?
Edit:I had successfully added a subview for the controller which extends TTThumbViewcontroller. Now i have to add a subview above the TTPhotoViewController toolbar (as in the attached image).
thanks in advance.
Ram
The banner view is nothing different than a normal view. So you could do the same with any other view. Following is the code I use in my app (I have the same banner view at the bottom of the screen, you can adjust its position to put in above the tab bar):
- (void)viewDidLoad{
[super viewDidLoad];
//we don't want user to see the ads at the very first load.
//once, it succeeds, it will be animated up by the bannerViewDidLoadAd
adView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, 480, 320, 50)];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifier320x50;
[adView setDelegate:self];
[self.view addSubview:adView];
self.bannerIsVisible = NO;
}
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOn" context:NULL];
adView.frame = CGRectMake(0, 346, 320, 50);
[UIView commitAnimations];
self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (self.bannerIsVisible)
{
[UIView beginAnimations:#"animateAdBannerOff" context:NULL];
adView.frame = CGRectMake(0, 480, 320, 50);
[UIView commitAnimations];
self.bannerIsVisible = NO;
}
}
- (void)dealloc{
adView.delegate = nil;
[adView release];
[super dealloc];
}