W AppDelegate.h:
#import <iAd/iAd.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ADBannerView *_bannerView;
...
...
}
@property (nonatomic, retain) ADBannerView *_bannerView;
W 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];
}
...
}
W kontrolkach widoku, które trzeba dodać iAd, utwórz kontener UIView o nazwie "vwAd" i utwórz połączenie w pliku XIB, w którym chcesz wyświetlić 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];
}
Proszę również zapoznać się z przykładami iAdSuite od Apple. Oryginalny układ funkcji Animated można znaleźć w przykładach iAdSuite. Nie zapomnij dodać funkcji delegata z przykładu iAdSuite do kontrolera widoku.