(1):加上iAd框架
在項目設置裡的Build Phases的Link Binary With Libraries,加上iAd.Framework。
(2):在xib界面裡拖入一個ADBannerView
(3)修改ViewController的.h文件
加入#import <iAd/iAd.h>,並且加入委托 @interface ViewController : UIViewController<ADBannerViewDelegate>
在下面加入代碼:
{
IBOutlet ADBannerView *adView;
BOOL bannerIsVisible;
}
@property (nonatomic, retain) ADBannerView *adView;
@property (nonatomic,assign)BOOL bannerIsVisible;
(4) 修改ViewController的.m文件
在@implementation ViewController下面加入@synthesize adView; @synthesize bannerIsVisible;
在- (void)viewDidLoad中加入self.adView.delegate = self;
並加入iad的兩個方法:
-(void) bannerViewDidLoadAd:(ADBannerView *)banner
{
if(!self.bannerIsVisible)
{
[UIViewbeginAnimations:@"animateAdBannerOn" context:NULL];
banner.frame = CGRectOffset(banner.frame,0, -100);
[UIViewcommitAnimations];
self.bannerIsVisible =YES;
}
}
-(void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if(self.bannerIsVisible)
{
[UIViewbeginAnimations:@"animateAdBannerOff" context:NULL];
banner.frame = CGRectOffset(banner.frame,0, 100);
[UIViewcommitAnimations];
self.bannerIsVisible =NO;
}
}