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. Dataguard Content

    1.Dataguard环境设计的三个重要概念 1.1 Primary数据库 在Data Guard的环境中与Standby数据库对应的数据库即是Primary数据库,也就是Primary数据库正在运行 ...

  2. colspan在浏览器中失效的问题

    <table border=" style="border-collapse:collapse;"> <tr> <td>def< ...

  3. gdi写的2048

    //-------------------------------------------[头文件及引用]----------------------------------------------- ...

  4. [转] 三种Python下载url并保存文件的代码

    原文 三种Python下载url并保存文件的代码 利用程序自己编写下载文件挺有意思的. Python中最流行的方法就是通过Http利用urllib或者urllib2模块. 当然你也可以利用ftplib ...

  5. 犯罪构成三层次记忆口诀 zt

    犯罪构成三层次记忆口诀 2012-02-17 来源:为你辩护网 浏览次数:232 0 众所周知,犯罪构成“四要件”和犯罪构成“三层次”(“三阶层”)都是分析具体刑事案件的辅助性工具.犯罪构成四要件是指 ...

  6. HDU 5699 货物运输 二分判定

    转自:http://blog.csdn.net/jtjy568805874/article/details/51480479 #include <cstdio> #include < ...

  7. Java String StringBuffer StringBuilder

    String  字符串常量存储在常量区,每次追加操作会创建新的对象: StringBuffer  字符串变量  线程安全 在堆上创建,每次追加操作在原对象上进行操作:  速度 StringBuffer ...

  8. bzoj 2815 [ZJOI2012]灾难(构造,树形DP)

    [题意] 求把每个点删除后,不可达点的数目. [思路] 构造一棵“灭绝树”,要求这棵树满足如果删除根节点后则该子树内的所有结点都不可达.则答案为子树大小-1. 如何构造这棵“灭绝树”? 将原图拓扑排序 ...

  9. NOIP2013 货车运输

    3.货车运输 (truck.cpp/c/pas) [问题描述] A 国有 n 座城市,编号从 1 到 n,城市之间有 m 条双向道路.每一条道路对车辆都有重量限制,简称限重.现在有 q 辆货车在运输货 ...

  10. 【Kafka入门】搭建Kafka本地环境

    本博文介绍如何一步步搭建起Kafka本地环境. 下载Kafka 0.9.0.0 并配置软链接 下载好后,放入电脑本地安装目录,mac下我放在/usr/local下,解压Kafka. -0.9.0.0. ...