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. Gen_server行为分析与实践

    1.简介 Gen_server实现了通用服务器client_server原理,几个不同的客户端去分享服务端管理的资源(如图),gen_server提供标准的接口函数和包含追踪功能以及错误报告来实现通用 ...

  2. 在MFC[转载]在MFC状态栏显示时间 状态栏显示时间

    原文:在MFC状态栏显示时间,谢小哈皮儿 c/c++ vc 在mfc状态栏显示时间,在VC的控件中有个Status bar可以在窗体状态栏中添加日期和时间.其实通过简单的代码,你就能创建一个有时钟显示 ...

  3. 富文本HTML编辑器UEditor

    Baidu百度开源富文本HTML编辑器UEditor JS代码网 发表于: 2013-10-30 分类:HTML编辑器  点击:2133  UEditor是由百度web前端研发部开发所见即所得富文本H ...

  4. 判断A树是否包含B树结构

    题目:输入两棵二叉树A和B,判断B是不是A的子结构 分析:根据数的遍历方法,首先想到的是采用递归的方式要更简单些,树A从根节点进行遍历,首先判断与B的根节点值是否相等,如果相等则进行递归遍历验证,否则 ...

  5. NOIP2005 篝火晚会

    篝火晚会 (fire.pas/c/cpp) [问题描述] 佳佳刚进高中,在军训的时候,由于佳佳吃苦耐劳,很快得到了教官的赏识,成为了“小教官”.在军训结束的那天晚上,佳佳被命令组织同学们进行篝火晚会. ...

  6. uvalive 4670 Dominating Patterns

    在文本串中找出现次数最多的子串. 思路:AC自动机模板+修改一下print函数. #include<stdio.h> #include<math.h> #include< ...

  7. return遇到finally

    public class Test { public static void main(String[] args) { System.out.println("=============t ...

  8. Ngrok,一款可以帮助你展示网站和联网开发的工具

      使用Ngrok的目的就是为了可以让别人通过网络访问到自己本机上的项目 下面是一个简单的使用教程,详细的查看官网英文文档. 第一步: 登录官网:https://ngrok.com/   注册或者登录 ...

  9. HDU-4614 Vases and Flowers 线段树区间更新

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4614 线段树保存区间是否被覆盖以及区间的和即可,在询问的时候在线段树上二分查找就可以了...代码写得比 ...

  10. hdu 5505 GT and numbers

    GT and numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...