In AppDelegate.h:
#import <iAd/iAd.h> @interface AppDelegate : UIResponder <UIApplicationDelegate> { ADBannerView *_bannerView; ... ... } @property (nonatomic, retain) ADBannerView *_bannerView;
In 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 UIView container named "vwAd" and create a connection in the 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]; }
Also check out Apple's iAdSuite examples. The original layout. The designated function can be found in iAdSuite examples. Remember to add the delegate functions from the iAdSuite example to your view controller.
source share