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. Android学习系列(20)--App数据格式之解析Json

    JSON数据格式,在Android中被广泛运用于客户端和网络(或者说服务器)通信,非常有必要系统的了解学习.     恰逢本人最近对json做了一个简单的学习,特此总结一下,以飨各位.     为了文 ...

  2. Kettle定时执行(ETL工具)【转】

    1,Kettle跨平台使用.    例如:在AIX下(AIX是IBM商用UNIX操作系统,此处在LINUX/UNIX同样适用),运行Kettle的相关步骤如下:    1)进入到Kettle部署的路径 ...

  3. Spring整合Hessian

    Spring让Hessian变得不但强大,而且易用,但是易用背后,却有不少陷阱!   这个例子很简单,但实际上的确花费了我超过一小时的时间,排除了种种问题,最后问题终于水落石出.   整合以上篇Hel ...

  4. Log Explorer使用说明

    一.介绍 Log Explorer主要用于对MSSQLServer的事物分析和数据恢复.你可以浏览日志.导出数据.恢复被修改或者删除的数据(包括执行过update,delete,drop和trunca ...

  5. 流(stream)

    流的概念 Java程序通过流来完成输入/输出.流是生产或消费信息的抽象.流通过Java的输入/输出系统与物理设备链接.尽管与它们链接的物理设备不尽相同,所有流的行为具有同样的方式.这样,相同的输入/输 ...

  6. Java HashMap实例源码分析

    引言 HashMap在键值对存储中被经常使用,那么它到底是如何实现键值存储的呢? 一 Entry Entry是Map接口中的一个内部接口,它是实现键值对存储关键.在HashMap中,有Entry的实现 ...

  7. [iOS基础控件 - 2] 按钮的基本使用

    UIButton A.素材准备 1.图片素材放置到Images.xcassets中     B.按钮状态 1.normal:默认状态 Default 对应的枚举常量:UIControlStateNor ...

  8. URL编码原理解释

    当你在浏览器中输入一个URL时,浏览器会将你输入到地址栏的非数字字母转化为URI编码. 那么,它是按照什么样的规则来转换的呢 是这样的,URI编码就是一个字符的ASCII码,它的ACSII码的十六进制 ...

  9. Keil Mdk5.0 破解包 和谐包【worldsing笔记】

    有关Keil MDK 5.0的介绍和下载 http://www.cnblogs.com/worldsing/p/3355911.html 下载地址 点击下载:http://pan.baidu.com/ ...

  10. UITableView 详解 教程

    看TableView的资料其实已经蛮久了,一直想写点儿东西,却总是因为各种原因拖延,今天晚上有时间静下心来记录一些最近学习的TableView的知识.下面进入正题,UITableView堪称UIKit ...