http://www.mwebb.me.uk/2013/08/adding-iads-to-cocos2d-x-on-ios.html

Looking at the forums it seems a lot of people are having trouble getting iADs working on Cocos2d-x on iOS. It's actually pretty straight forward, but I guess a lot of people using Cocos2d-x aren't familiar with Objective C.

Here's a quick explaination.

First, you need to add the iADFramework your project (click your targer, got to Summary, find Link Frameworks and Libraries and add it).

Now you need to add the code. In project explore, expand the folder with the same name as your application, and you'll see a folder (or group) call iOS. In there is the AppController clash. This is the place you need to put the code.

First, in AppController.h

Add an import for iAds.

#import <iAd/iAd.h>

Now you need to make the class a delegate for ADBannerViewDelegate as it needs to response to callbacks.

So my interface declaration looks like this now:

@interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate,UITextFieldDelegate,UIApplicationDelegate, ADBannerViewDelegate>{

OK - see it's got ADBannerViewDelegate at the end?

Now you need add you view to the header, and also a variable to keep track of whether your add is visible (as Apple with reject it if you don't hide non-filled iAD views)

So my header now looks like this:

#import <iAd/iAd.h>
@class RootViewController;
@interface AppController : NSObject <UIAccelerometerDelegate, UIAlertViewDelegate,UITextFieldDelegate,UIApplicationDelegate, ADBannerViewDelegate,  GADBannerViewDelegate >{
  UIWindow *window;
  RootViewController *viewController;
  ADBannerView *adView;
  bool bannerIsVisible;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) RootViewController *viewController;
@property (nonatomic, retain) ADBannerView *adView;
@property bool bannerIsVisible;
@end

OK, that's it for the header. Now go to the AppController.mm

Firstly, at the top, just under implementation, do your synthesising:

@synthesize adView;
@synthesize bannerIsVisible;

Now you need to create an ad banner, and add it to your root view controller. Find the lines:

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

That creates the viewController that you need to add your ad view to.

I want to add my add at the bottom, so this is the code I use:

adView = [[ADBannerView alloc]initWithFrame:CGRectZero];
adView.delegate = self;
[viewController.view addSubview:adView];
self.bannerIsVisible = NO;
  
   CGRect adFrame = adView.frame;
adFrame.origin.y = viewController.view.frame.size.width+adView.frame.size.height;
;
  adView.frame = adFrame;

The first three lines create the ad, and the last three move it off the bottom of the screen (as it's not got an ad yet, so it needs to be hidden. I'll leave you to work out how to change the code if you want it off the top.

IAds will call either 'BannerViewDidLoadAd' or 'didFailToRecieveAdWithError'. You need to respond to these, either by move the view onto the screen (if it loaded an ad) or moving it off it didn't (I know it's off the screen on the first call, but this may be a later ad). Add these at the bottom (just before @end)

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
  
     if (!self.bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOn" context:NULL];
  // Assumes the banner view is just off the bottom of the screen.
  CGRect adFrame = adView.frame;
adFrame.origin.y = viewController.view.frame.size.width-adView.frame.size.height;
;
  adView.frame = adFrame;
[UIView commitAnimations];
  self.bannerIsVisible = YES;
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
  
  
  NSLog(error.description);
   if (self.bannerIsVisible)
{
[UIView beginAnimations:@"animateAdBannerOff" context:NULL];
  // Assumes the banner view is placed at the bottom of the screen.
banner., banner.frame.size.height);
[UIView commitAnimations];
  self.bannerIsVisible = NO;
}
}

That's all you have to do (other than enable iADs in iTunesConnect for your ad - that's pretty easy).

Remember to make sure your test device its connected to the net when you test otherwise nothing will happen!

Also, iADs test server often responds with errors - don't worry - this is normal.

Adding iAds to Cocos2d-x on iOS的更多相关文章

  1. 史上最全的常用iOS的第三方框架

    文章来源:http://blog.csdn.net/sky_2016/article/details/45502921 图像: 1.图片浏览控件MWPhotoBrowser       实现了一个照片 ...

  2. IOS常用第三方开源类库&组件

    1.AFNetworking AFNetworking 采用 NSURLConnection + NSOperation, 主要方便与服务端 API 进行数据交换, 操作简单, 功能强大, 现在许多人 ...

  3. 常用iOS的第三方框架

    图像:1.图片浏览控件MWPhotoBrowser       实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等 ...

  4. iOS知名第三方框架和流行APP们所用的第三方框架小结

    网易新闻AppleReachabilityASIHTTPRequestEGOTableViewPullRefreshGTMNSString+HTMLMGTemplateEngineMPOAuthReg ...

  5. Delphi IOS环境安装

    RAD Delphi XE/10 Seattle 安装IOS.OSX环境安装,IOS模拟器,MAC X 真机可以调试 http://community.embarcadero.com/blogs/en ...

  6. iOS开发——网络编程Swift篇&(八)SwiftyJSON详解

    SwiftyJSON详解 最近看了一些网络请求的例子,发现Swift在解析JSON数据时特别别扭,总是要写一大堆的downcast(as?)和可选(Optional),看?号都看花了.随后发现了这个库 ...

  7. iOS第三方开源库的吐槽和备忘(转)

    原文:http://www.cocoachina.com/industry/20140123/7746.html 做iOS开发总会接触到一些第三方库,这里整理一下,做一些吐槽.   目前比较活跃的社区 ...

  8. Cocos2d中update与fixedUpdate的区别(六)

    它如何工作呢? update:和fixedUpdate:方法实际这样工作. Cocos2D将从iOS接口中取得时间间隔(delta)在你的游戏代码执行期间,并且检查fixedUpdate:方法在间隔期 ...

  9. iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角)--(转)

    图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩放等操作. 下 ...

随机推荐

  1. 【转】@Override must override a superclass method 问题解决

    原文网址:http://www.blogjava.net/anchor110/articles/339352.html 如果在使用Eclipse开发Java项目时,在使用 @Override 出现以下 ...

  2. ajax检测账户是否存在

    Register.cshtml <div title="账户"> 账户 <input type="text" name="Acc_a ...

  3. Ext入门学习系列(二)弹出窗体

    第二章 弹出窗体 上节学习了Ext的环境搭建和最基本的一个操作——弹出对话框,作为一个引子,本节讲述如何弹出一个新窗体,从实例讲解Ext的基本运行原理. 一.Ext的窗体长什么样? 先来看看几个效果, ...

  4. click事件的参数化

    Browser("XXX").Page("XXX").Frame("iframe_main").WebElement("TB-50 ...

  5. MapReduce:Shuffle过程的流程

    Shuffle过程是MapReduce的核心,Shuffle描述着数据从map task输出到reduce task输入的这段过程. 1.map端

  6. 【转载】extern "C"的用法解析(原博主就是抄百度百科的,不如另外一篇好)

    [说明]文章转载自Rollen Holt 的文章 http://www.cnblogs.com/rollenholt/archive/2012/03/20/2409046.html --------- ...

  7. 关于Java线程

    1 概念 通常来说,我们编写的Java代码是以进程的形式来运行的,所编写的代码就是“程序”,而执行中的程序就是“进程”.进程是系统进行资源分配和调度的独立单位. 线程是位于进程的下一级,是系统中的最小 ...

  8. FIREDAC数据引擎

    以前使用过BDE.ADO.DBX等数据引擎,后来发现它们都没有UNIDAC好用, 所以在很长的一段时间内中间件都使用UNIDAC作为数据引擎. 偶然的机会,使用了DELPHI XE5自带的FIREDA ...

  9. 转载 C#中使用结构来传递多个参数

    C#中当参数超过5个时,建议用结构来传递多个参数. 示例代码如下: public struct MyStruct { public string str; public int number; } c ...

  10. MSSQLSERVER数据库- SP_EXECUTESQL的使用

    EXEC和SP_EXECUTESQL有什么区别呢? 1,它们之间最大的区别是嵌入式的参数,如下面一个语句 declare @sql nvarchar() declare @id varchar() ' ...