iPhoneアプリにiAd広告が出るようにしてみた

echofonのiPhoneアプリなどでも画面の上のほうに広告バーが出ていますが、あれを自作アプリに表示させてみました。
普通に出すだけならもう少し単純なんですが、
TableViewを使っている画面の下部分に常に出るようにしたかったので、
スクロールしても画面の外に出ていってしまわないように座標計算と再計算タイミングをちょっと工夫をしました。

※あと、なんか読み込み前の広告バナーは画面外に出しておいた方がいいらしいので、そうしています。(Appleの審査の時に見られるのかな?)
読み込みが成功したら実際に表示したい場所へ配置しています。

・hファイル

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface PanelTableViewController : UITableViewController <ADBannerViewDelegate>

@property (retain, nonatomic) ADBannerView *customAdView;

- (void)configureView;

@end

・mファイル

@implementation PanelTableViewController

@synthesize customAdView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self configureView];
}

- (void)configureView
{
    // 広告バナー
    self.customAdView = [[ADBannerView alloc] initWithFrame:CGRectMake(0.0, 480.0, 0.0, 0.0)];
    self.customAdView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    self.customAdView.delegate = self;
    [self.view addSubview:customAdView];

    // 一番下のTableViewCellが広告バナーに隠れてしまわないように空のフッター設置
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 0.0, self.customAdView.frame.size.height)];

}

// delegateメソッド
// スクロールされる度に呼ばれる
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    float viewHeight = frame.size.height;
    float adViewWidth = self.customAdView.frame.size.width;
    float adViewHeight = self.customAdView.frame.size.height;
    float navBarHeight = self.navigationController.navigationBar.frame.size.height;
    self.customAdView.center = CGPointMake(adViewWidth / 2, self.tableView.contentOffset.y + viewHeight - navBarHeight - adViewHeight / 2);
    [self.view bringSubviewToFront:self.customAdView];
}

// delegateメソッド
// 広告バナーの読み込みに成功したとき呼ばれる
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
    CGRect frame = [[UIScreen mainScreen] applicationFrame];
    float viewHeight = frame.size.height;
    float adViewWidth = self.customAdView.frame.size.width;
    float adViewHeight = self.customAdView.frame.size.height;
    float navBarHeight = self.navigationController.navigationBar.frame.size.height;
    [UIView animateWithDuration:2.0
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseInOut
                     animations:^{
                         self.customAdView.center = CGPointMake(adViewWidth / 2, self.tableView.contentOffset.y + viewHeight - navBarHeight - adViewHeight / 2);
                         self.customAdView.alpha = 1.0;
                     }
                     completion:nil];
}

// delegateメソッド
// 広告バナーの読み込みに失敗したとき呼ばれる
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
    NSLog(@"広告表示が失敗しました");
}