本范例只着重在如何使用 ADBannerView 并没有 iAD 的相关设定与申请流程,至于其他行动广告的部份大家可以参考行动广告 Banner!让你赚大钱!一文。
首先汇入 iAD Framework,并引用其标头档,Xcode 4 Framework 汇入方式可以参考 Xcode 4 新增 Framework 的方法一文。
- #import <iAd/iAd.h>
#import <iAd/iAd.h>
接着在要使用此物件的类别上设定 代理,并建立一个 ADBannerView 型态的物件。
- @interface AdBannerViewViewController : UIViewController {
- ADBannerView *bannerView;
- }
@interface AdBannerViewViewController : UIViewController { ADBannerView *bannerView; }
在功能方面,我们希望 ADBannerView 能够与画面的呈现方式一致(直立与倾置),并且在 ADBannerView 准备好时能自动弹出显示,使用者也能自行切换是否要显示此 ADBannerView。
针对上述这些功能对 ADBannerView 进行初始化的动作,其程式码如下。
- //自定义的函式 Banner初始化(以画面直立)
- - (void)initializeBanner {
- //以画面直立的方式设定Banner于画面底部
- bannerView = [[ADBannerView alloc]initWithFrame:CGRectMake(0.0, 430.0, self.view.frame.size.width, 50.0)];
- //此Banner所能支援的类型
- bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
- //目前的Banner 类型
- bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
- //设定代理
- bannerView.delegate = self;
- //无法按下触发广告
- bannerView.userInteractionEnabled = NO;
- //设定偏位移量
- bannerView.frame = CGRectOffset(bannerView.frame, 0, 50);
- [self.view addSubview:bannerView];
- }
//自定义的函式 Banner初始化(以画面直立) - (void)initializeBanner { //以画面直立的方式设定Banner于画面底部 bannerView = [[ADBannerView alloc]initWithFrame:CGRectMake(0.0, 430.0, self.view.frame.size.width, 50.0)]; //此Banner所能支援的类型 bannerView.requiredContentSizeIdentifiers = [NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil]; //目前的Banner 类型 bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; //设定代理 bannerView.delegate = self; //无法按下触发广告 bannerView.userInteractionEnabled = NO; //设定偏位移量 bannerView.frame = CGRectOffset(bannerView.frame, 0, 50); [self.view addSubview:bannerView]; }
现在我们有了一个 ADBannerView,不过他的目前因为偏移量的关系在画面之外,暂时无法看到,所以下面程式码使用动画的方式将 ADBannerView 移动到画面内,制作出类似弹出的效果。
- - (void)bannerViewAnimation {
- //动画设定
- [UIView beginAnimations:@"BannerViewAnimation" context:NULL];
- //以userInteractionEnabled状态排判断bannerView是否在画面内
- if (bannerView.userInteractionEnabled) {
- bannerView.frame = CGRectOffset(bannerView.frame, 0, 50);
- }
- else {
- bannerView.frame = CGRectOffset(bannerView.frame, 0, -50);
- }
- //开始动画
- [UIView commitAnimations];
- //将userInteractionEnabled做反向设定
- bannerView.userInteractionEnabled = !bannerView.userInteractionEnabled;
- }
- (void)bannerViewAnimation { //动画设定 [UIView beginAnimations:@"BannerViewAnimation" context:NULL]; //以userInteractionEnabled状态排判断bannerView是否在画面内 if (bannerView.userInteractionEnabled) { bannerView.frame = CGRectOffset(bannerView.frame, 0, 50); } else { bannerView.frame = CGRectOffset(bannerView.frame, 0, -50); } //开始动画 [UIView commitAnimations]; //将userInteractionEnabled做反向设定 bannerView.userInteractionEnabled = !bannerView.userInteractionEnabled; }
上述程式码使用 userInteractionEnabled 这个参数来当作判断 ADBannerView 是否存在于画面内的依据,并且使用类似开关的技巧,每一次执行该函式都会得到一个反向的结果。由于我们希望 ADBannerView 准备好时会自动弹出来,以及使用者也可以自行切换它,所以必须在下列两个事件中呼叫上述函式。
- //使用者按钮事件
- - (IBAction)onButtonPress:(id)sender {
- [self bannerViewAnimation];
- }
- //当ADBannerView完成读取广告时会触发的事件
- - (void)bannerViewDidLoadAd:(ADBannerView *)banner {
- [self bannerViewAnimation];
- }
//使用者按钮事件 - (IBAction)onButtonPress:(id)sender { [self bannerViewAnimation]; } //当ADBannerView完成读取广告时会触发的事件 - (void)bannerViewDidLoadAd:(ADBannerView *)banner { [self bannerViewAnimation]; }
现在还剩下最后一个功能,就是 ADBannerView 能够与画面的呈现方式一致(直立与倾置),我们可以使用下列函式来重新设定 ADBannerView 的位置与他的类型。
- //画面转动时呼叫的内建函式
- - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
- //还原bannerView预设值
- bannerView.userInteractionEnabled = NO;
- bannerView.frame = CGRectOffset(bannerView.frame, 0,0);
- //判断画面是否倾置
- if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) {
- bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
- bannerView.frame = CGRectMake(0.0, 288.0, self.view.frame.size.width, 32.0);
- }
- else {
- bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
- bannerView.frame = CGRectMake(0.0, 430.0, self.view.frame.size.width, 50.0);
- }
- //重新设定偏移量
- bannerView.frame = CGRectOffset(bannerView.frame, 0, 50);
- }
//画面转动时呼叫的内建函式 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { //还原bannerView预设值 bannerView.userInteractionEnabled = NO; bannerView.frame = CGRectOffset(bannerView.frame, 0,0); //判断画面是否倾置 if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation)) { bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape; bannerView.frame = CGRectMake(0.0, 288.0, self.view.frame.size.width, 32.0); } else { bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; bannerView.frame = CGRectMake(0.0, 430.0, self.view.frame.size.width, 50.0); } //重新设定偏移量 bannerView.frame = CGRectOffset(bannerView.frame, 0, 50); }
最后,不要忘记在程式进入点引用 ADBannerView 初始化函式,并在对应的函式中释放其记忆体。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- [self initializeBanner];
- }
- (void)viewDidLoad { [super viewDidLoad]; [self initializeBanner]; }
- - (void)dealloc
- {
- [bannerView release];
- [super dealloc];
- }
- (void)dealloc { [bannerView release]; [super dealloc]; }
另外下面提供几个跟 ADBannerView 有关的内建函式。
- //ADBannerView发生错误时会触发
- - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {}
//ADBannerView发生错误时会触发 - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error {}
- //按下广告时会触发的函式
- -(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
- //这里可以放置暂停你应用程式的程式码
- return YES;
- }
//按下广告时会触发的函式 -(BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { //这里可以放置暂停你应用程式的程式码 return YES; }
- //重广告中按下返回应用程式按钮时所触发的函式
- - (void)bannerViewActionDidFinish:(ADBannerView *)banner {
- //这里可以放置重启你应用程式的程式码
- }
//重广告中按下返回应用程式按钮时所触发的函式 - (void)bannerViewActionDidFinish:(ADBannerView *)banner { //这里可以放置重启你应用程式的程式码 }
ps:在测试 ADBannerView 必须要连上网际网路才行,有时候测试用的 iAD 会因为连线烦繁忙等问题无法取得测试用广告,可能需要多执行几次。
来源:
http://furnacedigital.blogspot.com/2011/09/adbannerview.html
参考: