画面上部に表示させる場合と、画面下部に表示させる場合の2通りを示す。
HogeLayer.h
#import
#import "cocos2d.h"
#import "GADBannerView.h"
@interface HogeLayer : CCLayer <GADBannerViewDelegate>{
}
@end
画面上部にAdMobを表示させる場合
HogeLayer.m
@implementation HogeLayer{
GADBannerView* _admobView;
}
-(void) onEnter
{
_admobView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner];
_admobView.adUnitID = @"Your Publisher ID";
_admobView.rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[[[CCDirector sharedDirector] view] addSubview:_admobView];
_admobView.delegate = self;
GADRequest *request = [GADRequest request];
request.testing = NO;
[_admobView loadRequest:request];
}
画面下部にAdMobを表示させる場合
HogeLayer.m
@implementation HogeLayer{
GADBannerView* _admobView;
}
-(void) onEnter
{
CGRect rect = CGRectMake(0, winSize.height - GAD_SIZE_320x50.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height);
_admobView = [[GADBannerView alloc] initWithFrame:rect];
_admobView.adUnitID = @"Your Publisher ID";
_admobView.rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
[[[CCDirector sharedDirector] view] addSubview:_admobView];
_admobView.delegate = self;
GADRequest *request = [GADRequest request];
request.testing = NO;
[_admobView loadRequest:request];
}
画面上部に表示させる場合は、GADBannerViewをallocするときにinitWithAdSizeを指定するだけでよい。
画面下部に表示させる場合は、GADBannerViewをallocするときにinitWithFrameで表示領域を指定する。
参考記事:
http://narudesign.com/devlog/cocos2d-implement-admob/
http://blog.livedoor.jp/second_match/archives/51894540.html
http://tanukichi566.blog.fc2.com/blog-entry-61.html