cocos2dx-3.9 在iOS上集成admob
Part 1: 安装GoogleMobileAds framework (即admob)
source 'https://github.com/CocoaPods/Specs.git'
platform :ios,'7.0'
pod 'Google-Mobile-Ads-SDK','~> 7.5'
source 指定pod从哪里去下载新的framework pod一行指定取哪个版本的sdk
Part2: 使用admob 在此之前,你应该先去读一下 https://github.com/googleads/googleads-mobile-ios-examples 中的源码,看一下官方对Banner和Interstitial给出的示例。具体的代码照抄就可以了。 首先,官方案例是这么import framework的: @import GoogleMobileAds;
… bla bla~ 你可能会希望在.h中声明一个C++类,然后在一个.mm中引入GoogleMobileAds,实现一个内部类,然后再用一个struct桥接到C++以供C++代码调用?放弃这种方式吧,因为: @import语法只能在Objective-C中使用,目前Objective-C++不支持这种语法,哪怕你只是把文件扩展名改成.mm也不行 所以可以代替的办法是酱紫: #import <GoogleMobileAds/GoogleMobileAds.h>
基于一些以我目前水平无法解答的原因,我无法在一个.mm中引入GoogleMobileAds中的类,所以最后只好单独在一套.h/.m文件中实现一个Objective-C的类,然后在一个专门的.h/.mm中实现一个C++类桥接之。 关于桥接: C++类无法直接把一个Objective-C的类作为成员参数,所以一般办法是,在 .h中: 复制代码 struct AdBannerBridge;
class AdBanner
{
//...
AdBanner();
~AdBanner();
protected:
AdBannerBridge* _adBridge;
}
复制代码 复制代码 struct AdBannerBridge
{
GoogleAd* _googleAd;
AppleAd* _appleAd;
AdBannerBridge()
{
_appleAd = [[AppleAd alloc]init];
//...
}
~AdBannerBridge()
{
if( _appleAd != nil ) {
[_appleAd release];
}
//...
}
}
AdBanner::AdBanner()
{
_adBridge = new AdBannerBridge();
}
AdBanner::~AdBanner()
{
if( _adBridge ) {
delete _adBridge;
}
}
复制代码 GoogleMobileAds使用还是蛮简单的,解析example中的一部分代码: 复制代码 - (void)viewDidLoad {
[super viewDidLoad];
// Replace this ad unit ID with your own ad unit ID.
self.bannerView.adUnitID = @"ca-app-pub-3940256099942544/2934735716";
self.bannerView.rootViewController = self;
GADRequest *request = [GADRequest request];
// Requests test ads on devices you specify. Your test device ID is printed to the console when
// an ad request is made. GADBannerView automatically returns test ads when running on a
// simulator.
request.testDevices = @[
@"01d5d30957fabc2cce72c5559550686e" // Eric's iPod Touch
];
[self.bannerView loadRequest:request];
}
复制代码
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |