cocos2D 에서 iAd 및 AdMob 기능 추가 하는법(2가지 동시에)

iOS / AOS/cocos2D 2010. 9. 16. 11:50
http://www.cocos2d-iphone.org/forum/topic/8424
http://www.admob.com
애드몹을 사용 하시기 위해서는 위의 사이트에서 등록 하여 고유 넘버 키를 받으셔야 사용이가능 합니다.
참고 사이트 입니다.


=============================================================================
AdViewController.h
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import "AdMobDelegateProtocol.h"

@protocol AdViewControllerDelegate;

@interface AdViewController : UIViewController <ADBannerViewDelegate, AdMobDelegate>
{
	id <AdViewControllerDelegate> delegate;
	id adBannerView;

	AdMobView* adMobAd;
}

@property (nonatomic, assign) id <AdViewControllerDelegate> delegate;

- (id)initWithMasterView:(UIView*)masterView;
- (void)rotateBannerView:(UIDeviceOrientation)toDeviceOrientation;

@end

@protocol AdViewControllerDelegate

- (void)adController:(AdViewController*)controller didLoadiAd:(id)iadBanner;
- (void)adController:(AdViewController*)controller didFailedToRecieveiAd:(id)iadBanner;

- (void)adController:(AdViewController*)controller didLoadAdMobAd:(AdMobView*)adMobBanner;
- (void)adController:(AdViewController*)controller didFailedToRecieveAdMobAd:(AdMobView*)adMobBanner;

@end

AdViewController.m

#import "AdViewController.h"
#import "cocos2d.h"

#import "AdMobView.h"

@implementation AdViewController

@synthesize delegate;

- (UIDeviceOrientation)currentOrientation
{
	return [[CCDirector sharedDirector] deviceOrientation];
}

- (id) initWithMasterView:(UIView*) masterView
{
	if( nil != (self = [super init]) )
	{
		[self setView:masterView];

		//Initialize the class manually to make it compatible with iOS < 4.0
		Class classAdBannerView = NSClassFromString(@"ADBannerView");
		if (classAdBannerView != nil)
		{
			adBannerView = [[classAdBannerView alloc] initWithFrame:CGRectZero];
			[adBannerView setDelegate:self];
			[adBannerView setRequiredContentSizeIdentifiers: [NSSet setWithObjects:
															  ADBannerContentSizeIdentifier320x50,
															  ADBannerContentSizeIdentifier480x32, nil]];

			[self.view addSubview:adBannerView];

			[self rotateBannerView:[self currentOrientation]];

			[adBannerView setHidden:YES];

		}
		else
		{
			//Request an AdMob Ad
			adMobAd = [AdMobView requestAdOfSize:ADMOB_SIZE_320x48 withDelegate:self];
			[adMobAd retain];
		}
	}

	return self;
}

- (void)rotateBannerView:(UIDeviceOrientation)toDeviceOrientation
{
	if (adBannerView)
	{
		if (UIDeviceOrientationIsLandscape(toDeviceOrientation))
			[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier480x32];
		else
			[adBannerView setCurrentContentSizeIdentifier:ADBannerContentSizeIdentifier320x50];

		[(UIView*)adBannerView setTransform:CGAffineTransformIdentity];

		//Set the transformation for each orientation
		switch (toDeviceOrientation)
		{
			case UIDeviceOrientationPortrait:
			{
				[(UIView*)adBannerView setCenter:CGPointMake(160, 455)];
			}
				break;
			case UIDeviceOrientationPortraitUpsideDown:
			{
				[(UIView*)adBannerView setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(180))];
				[adBannerView setCenter:CGPointMake(160, 25)];
			}
				break;
			case UIDeviceOrientationLandscapeLeft:
			{
				[(UIView*)adBannerView setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90))];
				[adBannerView setCenter:CGPointMake(16, 240)];
			}
				break;
			case UIDeviceOrientationLandscapeRight:
			{
				[(UIView*)adBannerView setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(-90))];
				[adBannerView setCenter:CGPointMake(304, 240)];
			}
				break;
			default:
				break;
		}
	}

	if (adMobAd)
	{
		[adMobAd setTransform:CGAffineTransformIdentity];

		//Set the transformation for each orientation
		switch (toDeviceOrientation)
		{
			case UIDeviceOrientationPortrait:
			{
				[adMobAd setCenter:CGPointMake(160, 456)];
			}
				break;
			case UIDeviceOrientationPortraitUpsideDown:
			{
				[adMobAd setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(180))];
				[adMobAd setCenter:CGPointMake(160, 24)];
			}
				break;
			case UIDeviceOrientationLandscapeLeft:
			{
				[adMobAd setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(90))];
				[adMobAd setCenter:CGPointMake(24, 240)];
			}
				break;
			case UIDeviceOrientationLandscapeRight:
			{
				[adMobAd setTransform:CGAffineTransformMakeRotation(CC_DEGREES_TO_RADIANS(-90))];
				[adMobAd setCenter:CGPointMake(296, 240)];
			}
				break;
			default:
				break;
		}
	}
}

#pragma mark -
#pragma mark ADBannerViewDelegate

- (BOOL)allowActionToRun
{
	return YES;
}

- (void) stopActionsForAd
{
	//Pause background music here

	[adBannerView setHidden:YES];

	[[CCDirector sharedDirector] stopAnimation];
	[[CCDirector sharedDirector] pause];
}

- (void) startActionsForAd
{
	//Resume background music here

	[self rotateBannerView:[self currentOrientation]];
	[[UIApplication sharedApplication] setStatusBarOrientation:(UIInterfaceOrientation)[self currentOrientation]];
	[adBannerView setHidden:NO];

	[[CCDirector sharedDirector] stopAnimation];
	[[CCDirector sharedDirector] resume];
	[[CCDirector sharedDirector] startAnimation];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
	BOOL shouldExecuteAction = [self allowActionToRun];
    if (!willLeave && shouldExecuteAction)
    {
		[self stopActionsForAd];
    }
    return shouldExecuteAction;
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
	[adBannerView setHidden:NO];
	[delegate adController:self didLoadiAd:banner];
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
	[adBannerView setHidden:YES];
	[delegate adController:self didFailedToRecieveiAd:banner];
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
	[self startActionsForAd];
}

#pragma mark -
#pragma mark AdMobDelegate methods

- (NSString *)publisherIdForAd:(AdMobView *)adView
{
	return @"your_admob_publisher_id";    //replace it with you publisher id
}

- (UIViewController *)currentViewControllerForAd:(AdMobView *)adView {
	return self;
}

- (UIColor *)adBackgroundColorForAd:(AdMobView *)adView
{
	return [UIColor colorWithRed:0 green:0 blue:0 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (UIColor *)primaryTextColorForAd:(AdMobView *)adView
{
	return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (UIColor *)secondaryTextColorForAd:(AdMobView *)adView
{
	return [UIColor colorWithRed:1 green:1 blue:1 alpha:1]; // this should be prefilled; if not, provide a UIColor
}

- (NSArray *)testDevices
{
	return [NSArray arrayWithObjects: ADMOB_SIMULATOR_ID, nil];
}

- (void)didReceiveAd:(AdMobView *)adView
{
	CCLOG(@"AdMob: Did receive ad");

	[self rotateBannerView:[self currentOrientation]];
	[self.view addSubview:adMobAd];

	[delegate adController:self didLoadAdMobAd:adView];
}

// Sent when an ad request failed to load an ad
- (void)didFailToReceiveAd:(AdMobView *)adView
{
	CCLOG(@"AdMob: Did fail to receive ad");
	[adMobAd removeFromSuperview];
	[adMobAd release];
	adMobAd = nil;

	[delegate adController:self didFailedToRecieveAdMobAd:adView];
}

#pragma mark -
#pragma mark Memory Management

- (void) dealloc
{
	if (adMobAd)
	{
		[adMobAd setDelegate:nil];
		[adMobAd removeFromSuperview];
		[adMobAd release];
		adMobAd = nil;
	}

	if (adBannerView)
	{
		[adBannerView setDelegate:nil];
		[adBannerView removeFromSuperview];
		[adBannerView release];
		adBannerView = nil;
	}

    [super dealloc];
}

@end

The usage is very simple, #import AdViewController.h in you Layer's .h and add the delegate protocol AdViewControllerDelegate

#import "AdViewController.h"

@interface Menu : CCLayer <AdViewControllerDelegate>
{
	AdViewController	*adController;
}

And in your layer's .m add this

- (void)onEnter
{
	[super onEnter];

	adController = [[AdViewController alloc] initWithMasterView:[[CCDirector sharedDirector] openGLView]];
	[adController setDelegate:self];

}

- (void)onExit
{
	//Completely remove the controller
	[adController setDelegate:nil];
	[adController release];
	adController = nil;

	[super onExit];
}

- (void)adController:(AdViewController*)controller didLoadiAd:(id)iadBanner;
{
	//Do something when the ad loads, like moving objects.
}

- (void)adController:(AdViewController*)controller didFailedToRecieveiAd:(id)iadBanner;
{
	//Do something when the ad fails to load, like moving objects.;
}

- (void)adController:(AdViewController*)controller didLoadAdMobAd:(AdMobView*)adMobBanner;
{
	//Do something when the ad loads, like moving objects.
}

- (void)adController:(AdViewController*)controller didFailedToRecieveAdMobAd:(AdMobView*)adMobBanner;
{
	//Do something when the ad fails to load, like moving objects.
}
훌 이분 용자인듯 0.0