如何在Cocos2d-x游戏里集成iAd广告

Datetime:2016-08-23 04:32:08          Topic: Cocos2d-X           Share

1) Link binary with iAd.framework https://developer.apple.com/library/ios/#recipes/xcode_help-project_editor/Articles/AddingaLibrarytoaTarget.html

1. In the project navigator, select your project
2. Select your target
3.  Select the 'Build Phases' tab
4.  Open 'Link Binaries With Libraries' expander    5.  Click the '+' button
6.  Select iAd.framework

2) Download iAdSuite https://developer.apple.com/library/ios/#samplecode/iAdSuite/Introduction/Intro.html and copy files BannerViewController.h and BannerViewController.m from ContainerBanner project to your project.

3) Modify AppController.mm, add

<code><pre>#import "BannerViewController.h"</code></pre>

and replace

@implementationAppController

with

@implementationAppController
{
    BannerViewController*_bannerViewController;
}

and

// Set RootViewController to window
if([[UIDevicecurrentDevice].systemVersionfloatValue]< 6.0)
{
//
 warning: addSubView doesn't work on iOS6
	 [window addSubview:viewController.view];
}else
{
//use this method on ios6
	 [window setRootViewController:viewController];
}

with

_bannerViewController=[[BannerViewController alloc]initWithContentViewController:viewController];
window.rootViewController=_bannerViewController;

4) Modify RootViewController.mm, add

#import"BannerViewController.h"

and replace

/
//The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
-(id)initWithNibName:(NSString )nibNameOrNil bundle:(NSBundle )nibBundleOrNil {
 if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
//Custom initialization
}
 return self;
}
/
with
-(id)initWithNibName:(NSString)nibNameOrNil bundle:(NSBundle)nibBundleOrNil
 {
	if((self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])){
		[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(willBeginBannerViewActionNotification:)	name:BannerViewActionWillBegin object:nil];
		[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didFinishBannerViewActionNotification:)name:BannerViewActionDidFinisho bject:nil];
}
	return self;
}

and add the following methods

-(void)willBeginBannerViewActionNotification:(NSNotification)notification{
    NSLog(@“willBeginBannerViewActionNotification”);
}
-(void)didFinishBannerViewActionNotification:(NSNotification)notification
{
    NSLog(@“didFinishBannerViewActionNotification”);
}

In willBeginBannerViewActionNotification you can pause your game before the banner view triggers an advertising action. In didFinishBannerViewActionNotification you can resume your game when control is returned to it.

BannerViewController changes EAGLView size when banner becomes visible. If you use it like that, cocos2d-x may not detect touch location correctly. Here are the modifications to BannerViewController.m required to display ADBannerView on top of EAGLView.

1) Edit loadView method and put line

[contentView addSubview:_bannerView];

after

[contentView addSubview:_contentController.view];

loadView should look like this:

-(void)loadView
{
	UIView *contentView=[[UIView alloc]initWithFrame[[UIScreen mainScreen]bounds]];
   [self addChildViewController:contentController];
   [contentView addSubview:contentController.view];
   [contentController didMoveToParentViewController:self];
   [contentView addSubview:bannerView];
   self.view=contentView;
}
2) Edit viewDidLayoutSubviews method and replace
if(bannerView.bannerLoaded)
 {
	 contentFrame.size.height-=bannerFrame.size.height;
	 bannerFrame.origin.y=contentFrame.size.height;
}else{
	bannerFrame.origin.y=contentFrame.size.height;
}

with
if(bannerView.bannerLoaded)
 {
    bannerFrame.origin.y=0;
}else{
    bannerFrame.origin.y= -bannerFrame.size.height;
}
P.S. Are you sure you want to use iAd? Besides low fill rate, there are some limitations. For instance, Apple’s policy is not to serve iAd advertisements to applications that are designed for children as stated at https://developer.apple.com/support/ios/iad-network.html.




About List