I\'m having 5 ViewControllers in each of that i have to display iAd,so that i have to implement iAd code in each ViewControllers. Instead of that if i create
Just create a Pointer to iAD in APP delegate.
.h:
@property (strong, nonatomic) ADBannerView *UIiAD;
.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIiAD = [[ADBannerView alloc] init];
return YES;
}
Then in your ViewControllers do this:
.h:
@property (strong, nonatomic) ADBannerView *UIiAD;
.m:
- (AppDelegate *) appdelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (void) viewWillAppear:(BOOL)animated {
UIiAD = [[self appdelegate] UIiAD];
UIiAD.delegate=self;
// CODE to correct the layout
}
- (void) viewWillDisappear:(BOOL)animated{
UIiAD.delegate=nil;
UIiAD=nil;
[UIiAD removeFromSuperview];
}
Do this for all the view controllers with appropriate code to redesign the layout!
hi this looks like a good answer but don't you have to import the AppDelegate in your m files
#import "AppDelegate.h"
and shouldn't you hide the add if no network connection or add not showing
In the AppDelegate.h:
#import <iAd/iAd.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ADBannerView *_bannerView;
...
...
}
@property (nonatomic, retain) ADBannerView *_bannerView;
In the AppDelegate.m:
@synthesize _bannerView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) {
self._bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
self._bannerView = [[ADBannerView alloc] init];
}
...
}
In the view controllers that you need to add iAd, create a container UIView with name 'vwAd' and make the connection in xib file where you want to display iAds.
@interface ViewController : UIViewController<ADBannerViewDelegate>
{
IBOutlet UIView *vwAd;
...
...
}
- (void)layoutAnimated:(BOOL)animated
{
CGRect contentFrame = self.view.bounds;
if (contentFrame.size.width < contentFrame.size.height) {
self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect bannerFrame = self.appDelegate._bannerView.frame;
if (self.appDelegate._bannerView.bannerLoaded) {
bannerFrame.origin.y = 0;
} else {
bannerFrame.origin.y = vwAd.frame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
self.appDelegate._bannerView.frame = bannerFrame;
}];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
...
[self.appDelegate._bannerView removeFromSuperview];
self.appDelegate._bannerView.delegate = nil;
self.appDelegate._bannerView.delegate = self;
[vwAd addSubview:self.appDelegate._bannerView];
[self layoutAnimated:NO];
}
Please also review iAdSuite examples from Apple. Original layoutAnimated function can be found in iAdSuite examples. Do not forget to add the delegate functions from iAdSuite example into your view controller.