iDevGames Forums
How To Integrate iAd into iPhone App, wrong position of the banner - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: How To Integrate iAd into iPhone App, wrong position of the banner (/thread-9865.html)



How To Integrate iAd into iPhone App, wrong position of the banner - sefiroths - Feb 13, 2012 08:52 AM

i have a cocos2d app, that musta have a banner. i have seen this: http://www.raywenderlich.com/1371/how-to-integrate-iad-into-your-iphone-app

i have tried to use this example to make a view with only the banner, so i created a new UIViewController with xib, then add to a cocos2d view with:
Code:
banner=[[Banner alloc] initWithNibName:@"Banner" bundle:[NSBundle mainBundle]];
[[[CCDirector sharedDirector] openGLView] addSubview:[banner view]];

the unexpected result is: when i add this view, sometimes, randomly, the banner appears behind the status bar (partially covered by the status bar) and i can't explain why... the firs time the view appear is ALWAYS behind the status bar, so you'll see noted in code: //<<<<<<<<---------------------------------------note: i have putted this because the first time the window appear, it is alway behind the status bar

i have no contentView even if i have in this code, is not connected to any view
banner.h:
Code:
` #import #import "iAd/ADBannerView.h"

@interface Banner : UIViewController <UISplitViewControllerDelegate, ADBannerViewDelegate> {
   UIView *_contentView;
   id _adBannerView;
   BOOL _adBannerViewIsVisible;
    int firstTime;
}

@property (nonatomic, retain) IBOutlet UIView *contentView;
@property (nonatomic, retain) id adBannerView;
@property (nonatomic) BOOL adBannerViewIsVisible;

@end
banner.m
Code:
#import "Banner.h"

@implementation Banner

@synthesize contentView = _contentView;
@synthesize adBannerView = _adBannerView;
@synthesize adBannerViewIsVisible = _adBannerViewIsVisible;


- (int)getBannerHeight:(UIDeviceOrientation)orientation {
    if (UIInterfaceOrientationIsLandscape(orientation)) {
        return 32;
    } else {
        return 50;
    }
}

- (int)getBannerHeight {
    return [self getBannerHeight:[UIDevice currentDevice].orientation];
}

- (void)createAdBannerView {
    Class classAdBannerView = NSClassFromString(@"ADBannerView");
    if (classAdBannerView != nil) {
        self.adBannerView = [[[classAdBannerView alloc]
                       initWithFrame:CGRectZero] autorelease];
        [_adBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:
                                            ADBannerContentSizeIdentifier320x50,
                                            ADBannerContentSizeIdentifier480x32, nil]];
        if (UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier480x32];
        } else {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier320x50];          
        }
        [_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0,
                                  -[self getBannerHeight])];
        [_adBannerView setDelegate:self];

        [self.view addSubview:_adBannerView];      
    }
}

- (void)fixupAdView:(UIInterfaceOrientation)toInterfaceOrientation {
    if (_adBannerView != nil) {      
        if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier480x32];
        } else {
            [_adBannerView setCurrentContentSizeIdentifier:
          ADBannerContentSizeIdentifier320x50];
        }        
        [UIView beginAnimations:@"fixupViews" context:nil];
        if (_adBannerViewIsVisible) {
            CGRect adBannerViewFrame = [_adBannerView frame];
            adBannerViewFrame.origin.x = 0;
            adBannerViewFrame.origin.y = 0;
            if(firstTime==0){//<<<<-------------------------------------note: i have putted this because the first time the window appear, it is alway behind the status bar
                adBannerViewFrame.origin.y = 20;
                firstTime++;
            }
            [_adBannerView setFrame:adBannerViewFrame];
            CGRect contentViewFrame = _contentView.frame;
            contentViewFrame.origin.y =
         [self getBannerHeight:toInterfaceOrientation];
            contentViewFrame.size.height = self.view.frame.size.height -
         [self getBannerHeight:toInterfaceOrientation];
            _contentView.frame = contentViewFrame;
            NSLog(@"visible frame %f %f %f %f ",adBannerViewFrame.origin.x,adBannerViewFrame.origin.y,adBannerViewFrame.size.wi​dth,adBannerViewFrame.size.height);
        } else {
            CGRect adBannerViewFrame = [_adBannerView frame];
            adBannerViewFrame.origin.x = 0;
            adBannerViewFrame.origin.y = 0
         -[self getBannerHeight:toInterfaceOrientation];
            [_adBannerView setFrame:adBannerViewFrame];
            CGRect contentViewFrame = _contentView.frame;
            contentViewFrame.origin.y = 0;
            contentViewFrame.size.height = self.view.frame.size.height;
            _contentView.frame = contentViewFrame;
            NSLog(@"invisib frame %f %f %f %f ",adBannerViewFrame.origin.x,adBannerViewFrame.origin.y,adBannerViewFrame.size.wi​dth,adBannerViewFrame.size.height);
        }
        [UIView commitAnimations];
    }  
}


/*
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    NSLog(@"did load banner");

    firstTime=0;//<<<<<<<<---------------------------------------note: i have putted this because the first time the window appear, it is alway behind the status bar
   [self createAdBannerView];
    [super viewDidLoad];
}

- (void) viewWillAppear:(BOOL)animated {
    [self fixupAdView:[UIDevice currentDevice].orientation];
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrient​ation duration:(NSTimeInterval)duration {
    [self fixupAdView:toInterfaceOrientation];
}

#pragma mark ADBannerViewDelegate

- (void)bannerViewDidLoadAd:(ADBannerView *)banner {
    if (!_adBannerViewIsVisible) {              
        _adBannerViewIsVisible = YES;
        [self fixupAdView:[UIDevice currentDevice].orientation];
    }
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    if (_adBannerViewIsVisible)
    {      
        _adBannerViewIsVisible = NO;
        [self fixupAdView:[UIDevice currentDevice].orientation];
    }
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOr​ientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)dealloc {
    [super dealloc];
   self.contentView = nil;
   self.adBannerView = nil;  

}
@end

`
any ideas? thanks


RE: How To Integrate iAd into iPhone App, wrong position of the banner - ipeku - Feb 20, 2012 06:01 AM

[_adBannerView setFrame:CGRectOffset([_adBannerView frame], 0,
-[self getBannerHeight])];

This seems to set the frame of the banner. Run the program with the debugger and look at the values, for example, bannerHeight, when the problem occurs and when it doesn't.


RE: How To Integrate iAd into iPhone App, wrong position of the banner - sefiroths - Apr 3, 2012 01:00 AM

tryed but even if i put a fixed frame, it sometimes go to the wrong position Sad