1、 先去Google官网下载最新的IOS版本的SDK:https://developers.google.com/mobile-ads-sdk/download#downloadios
2、解压下载后的zip包,把包里面的文件全部添加到xcode项目IOS目录下。
3、引入开发框架。点击你具体的项目-->Build Phases标签-->Link Binary With Libraries,然后点击“+”添加以下几个开发框架:
EventKitUI(新版本:6.12.0需新增的库)
EventKit(新版本:6.12.0需新增的库)
CoreMedia(新版本:7.2.2需新增的库)
具体Xcode设置可参考官方说明:https://developers.google.com/mobile-ads-sdk/docs/admob/ios/quick-start
特别提醒:配置Xcode环境时,记得要将-ObjC
添加至应用目标构建设置中的Other Linker Flags:
1. 在Xcode的项目导航器中,按蓝色的顶级项目图标。
2. 点击目标,然后点击Build Settings标签。
3. 在Other Linker Flags下,将-ObjC
同时添加至Debug和Release。
如果不设置的话,运行时会报异常:
<code style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif;"><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">***</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="typ" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(43, 145, 175);">Terminating</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> app due to uncaught exception </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">'NSInvalidArgumentException'</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">,</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> reason</span><span class="pun" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;">:</span><span class="pln" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent;"> </span><span class="str" style="margin: 0px; padding: 0px; border: 0px; vertical-align: baseline; background-color: transparent; color: rgb(128, 0, 0);">'-[GADSlot state]: unrecognized selector sent to instance</span></code>
4、修改AppController.h文件,代码如下:
// Admob Banner广告 #import "GADBannerView.h" #import "GADBannerViewDelegate.h" @class RootViewController; @interface AppController : NSObject <UIApplicationDelegate, GADBannerViewDelegate> { UIWindow *window; RootViewController *viewController; // 将其中一个声明为实例变量 GADBannerView *bannerView_; } @end
5、初始化和显示Banner广告。打开AppController.mm文件,在didFinishLaunchingWithOptions函数中添加以下代码来对Banner进行初始化:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. // Add the view controller's view to the window and display. window = [[UIWindow alloc] initWithFrame: [[UIScreen mainScreen] bounds]]; // Init the EAGLView EAGLView *__glView = [EAGLView viewWithFrame: [window bounds] pixelFormat: kEAGLColorFormatRGB565 depthFormat: GL_DEPTH24_STENCIL8_OES preserveBackbuffer: NO sharegroup: nil multiSampling: NO numberOfSamples: 0]; // Use RootViewController manage EAGLView viewController = [[RootViewController alloc] initWithNibName:nil bundle:nil]; viewController.wantsFullScreenLayout = YES; viewController.view = __glView; // 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]; } // =================添加Admob Banner广告========== // 在屏幕顶部创建标准尺寸的视图。 // 在GADAdSize.h中对可用的AdSize常量进行说明。 // kGADAdSizeSmartBannerPortrait是Admob版本6.0.0之后支持智能横幅广告, bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait]; // 指定广告单元ID。 bannerView_.adUnitID = @"Your_Banner_ID"; //设置代理 [bannerView_ setDelegate:self]; // 告知运行时文件,在将用户转至广告的展示位置之后恢复哪个UIViewController // 并将其添加至视图层级结构。 bannerView_.rootViewController = viewController; [viewController.view addSubview:bannerView_]; // 启动一般性请求并在其中加载广告。 [bannerView_ loadRequest:[GADRequest request]]; // ===================End================= [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden:true]; cocos2d::CCApplication::sharedApplication()->run(); return YES; }
记得换上自己的Admob广告ID:
bannerView_.adUnitID = @"Your_Banner_ID";
其中的kGADAdSizeSmartBannerPortrait表示横幅广告的宽度会自动根据屏幕的宽度去自动适应(包括:横屏和竖屏的时候),具体可以参考官方说明:https://developers.google.com/mobile-ads-sdk/docs/admob/smart-banners。
//设置代理 [bannerView_ setDelegate:self];还有这句代码的意思,其实说:为bannerView_添加一个监听,也就是上面AppContoller.h里面继承的GADBannerViewDelegrate类。
6、实现GADBannerViewDelegrate类的两个回调函数:adViewDidReceiveAd 和 didFailToReceiveAdWithError,具体代码如下:
//实现GADBannerView的代理函数 - (void)adViewDidReceiveAd:(GADBannerView *)view { // 设置Banner的位置 CGRect contentFrame = viewController.view.bounds; CGRect bannerFrame = CGRectZero; #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 bannerFrame = _bannerView.frame; #else bannerFrame.size = [bannerView_ sizeThatFits:contentFrame.size]; #endif bannerFrame.origin.x = (contentFrame.size.width - bannerFrame.size.width) / 2; // Banner广告将显示在最底部 contentFrame.size.height -= bannerFrame.size.height; bannerFrame.origin.y = contentFrame.size.height; // 这行代码会改变屏幕的高度,等于是广告的Banner和游戏画面按照纵向的线性布局了 // viewController.view.frame = contentFrame; // 更改Banner位置 bannerView_.frame = bannerFrame; } // 获取Admob Banner广告失败后的回调函数 -(void)adView:(GADBannerView *)view didFailToReceiveAdWithError:(GADRequestError *)error { [view setHidden:YES]; NSLog(@"Failed to receive ad %@", [error localizedDescription]); // 设置Banner的位置 CGRect contentFrame = viewController.view.bounds; CGRect bannerFrame = CGRectZero; #if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0 bannerFrame = _bannerView.frame; #else bannerFrame.size = [bannerView_ sizeThatFits:contentFrame.size]; #endif bannerFrame.origin.x = (contentFrame.size.width - bannerFrame.size.width) / 2; bannerFrame.origin.y = contentFrame.size.height; // 更改Banner位置 bannerView_.frame = bannerFrame; }
- (void)dealloc { // 如果您在项目中使用ARC,请勿发布bannerView_ [bannerView_ release]; [window release]; [super dealloc]; }