iPhoneアプリにAdMob広告を追加する方法
ここでは、プログラム的に追加する(Interface Builderを一切使わない)方法を説明します。
こちらの環境は以下の通りです。
Mac OS X 10.6.3
Xcode 3.2.2
iPhoneシミュレータ 3.1.3
プロジェクトのセットアップ
Xcodeで、プロジェクトを作成します(View-based Applicationを選択)。
プロジェクト名は、「AdMob」としました。
Frameworksグループを右クリックして[追加] -> [既存のフレームワーク]で、以下のフレームワークを追加します。
- AudioToolbox.framework
- QuartzCore.framework
- MediaPlayer.framework
- MessageUI.framework
続いて、AdMobのサイトでダウンロードしてきたiPhone用のAdMobのSDKに含まれている以下の二つをプロジェクトに追加します(FinderからXcodeプロジェクトにドラッグアンドドロップで追加できます)。
- AdMob
- TouchJSON
このとき、「デスティネーショングループのフォルダに項目をコピーする(必要な場合)」にチェックが入っていることを確認し、[追加]を選択します。
InterFace Builderを使わないための設定
Resourceの中にある以下の2ファイルを削除します。
- AdMobViewController.xib
- MainWindow.nib
そして、Admob-info.plistの中の「Main nib file base name」という行を削除します。
続いて、Other Sources/main.mを以下のように書きかえます。
int retVal = UIApplicationMain(argc, argv, nil, nil);
int retVal = UIApplicationMain(argc, argv, nil, @"AdMobAppDelegate");
そして、Classes/AdMobAppDelegate.mを以下のように書きかえます。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
↓
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect frame = [[UIScreen mainScreen] bounds];
window = [[UIWindow alloc] initWithFrame:frame];
viewController = [[AdMobViewController alloc] init];
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
return YES;
}
実装
実際にAdMobを組み込む処理を追加します。AdMobのSDKに含まれているサンプルを参考にしました。
Classes/AdMobViewController.h
#define AD_REFRESH_PERIOD 12.5 // display fresh ads every 12.5 seconds
#import <UIKit/UIKit.h>
#import "AdMobView.h"
#import "AdMobAppDelegate.h"
#import "AdMobDelegateProtocol.h"
@interface AdMobViewController : UIViewController<AdMobDelegate> {
AdMobView *adMobAd;
NSTimer *refreshTimer;
}
@end
Classes/AdMobViewController.m
#import "AdMobViewController.h"
@implementation AdMobViewController
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
// get the window frame here.
CGRect appFrame = [UIScreen mainScreen].applicationFrame;
UIView *view = [[UIView alloc] initWithFrame:appFrame];
// making flexible because this will end up in a navigation controller.
view.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.view = view;
[view release];
// Request an ad
adMobAd = [AdMobView requestAdWithDelegate:self]; // start a new ad request
[adMobAd retain]; // this will be released when it loads (or fails to load)
}
- (NSString *)publisherId {
return @"a14bd9a27f5f572"; // this should be prefilled; if not, get it from www.admob.com
}
- (UIViewController *)currentViewController {
return self;
}
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)didReceiveAd:(AdMobView *)adView {
NSLog(@"AdMob: Did receive ad");
// get the view frame
CGRect frame = self.view.frame;
// put the ad at the bottom of the screen
adMobAd.frame = CGRectMake(0, frame.size.height - 48, frame.size.width, 48);
[self.view addSubview:adMobAd];
[refreshTimer invalidate];
refreshTimer = [NSTimer scheduledTimerWithTimeInterval:AD_REFRESH_PERIOD target:self selector:@selector(refreshAd:) userInfo:nil repeats:YES];
}
- (void)refreshAd:(NSTimer *)timer {
[adMobAd requestFreshAd];
}
// Sent when an ad request failed to load an ad
- (void)didFailToReceiveAd:(AdMobView *)adView {
NSLog(@"AdMob: Did fail to receive ad");
[adMobAd removeFromSuperview]; // Not necessary since never added to a view, but doesn't hurt and is good practice
[adMobAd release];
adMobAd = nil;
// we could start a new ad request here, but in the interests of the user's battery life, let's not
}
- (void)dealloc {
[super dealloc];
}
@end
いざ実行!
実行してみるとこんな感じになると思います。

下の方に広告が表示されていますね!







