cocos2d-x如何控制admob广告在ios界面显示

最近在cocos2d-x 3.X上加ios的admob广告,各种悲剧,有没有比较好的教程链接发一篇过来。我主要需要IOS上集成admob,然后可以控制在游戏的各个界面控制显示或不显示广告,插屏广告。简单加到controller显示admob的我会的。

最后还是自己搞定,看下面我的回复。

哎,求人不如求己,网上搜了n篇都是最基本了,论坛里也没有搜到。那我就写一篇吧。:11::11::11:

网上看到一个家伙转的,有点像,结果看到最后只有一个思路而已,估计转的博主自己也没弄明白。

http://blog.csdn.net/ym19860303/article/details/8840802

最后说下我的实现:
思路创建一个uiview单例来保存admob banner,方便后面控制它。
代码如下:
#import “GADBannerView.h”

#define ADENABLE 1

@interface AdController : UIView{
GADBannerView *_banner;
CGRect _frame;
}

  • (id)initWithFrame:(CGRect)frame rootController:(UIViewController *)controller;
  • (void)showAdAtTop;
  • (void)showAdAtBottom;

+(AdController *)shared_AdController;

@end

#import “AdController.h”

#define IADVIEWHEIGHT 48
#define ADMOBID @“111111111111111111”;

@implementation AdController

static AdController *_shared_AdController = nil;

+(AdController *)shared_AdController
{
if(!_shared_AdController)
{
_shared_AdController =init];
}
return _shared_AdController;
}

+(id)alloc
{
NSAssert(_shared_AdController == nil,@“Attempted to allocate a second instance of a singleton.”);
return ;
}

-(id) init
{
if(self = )
{

}
return self;

}

  • (id)initWithFrame:(CGRect)frame rootController:(UIViewController *)controller
    {
    if (self =) {
    _frame = frame;
    _banner = initWithFrame:CGRectMake(0, frame.size.height - IADVIEWHEIGHT, frame.size.width, IADVIEWHEIGHT)];
    _banner.adUnitID = ADMOBID;

      _banner.rootViewController = controller;
      ;
      ;
      ;
      GADRequest *request = ;
      ;
    

    }
    return self;
    }

#pragma mark GADBannerViewDelegate impl

  • (void)adViewDidReceiveAd:(GADBannerView *)adView {
    NSLog(@“Received admob successfully”);
    ;
    }

  • (void) receiveADmob
    {

}

  • (void)adView:(GADBannerView *)view
    didFailToReceiveAdWithError:(GADRequestError *)error {
    NSLog(@“Failed to receive ad with error: %@”, );
    }

  • (void)showAdAtTop
    {
    ;
    }

  • (void)showAdAtBottom
    {
    ;
    }

@end

然后把单例在appcontroller里创建

代码如下:

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    cocos2d::Application *app = cocos2d::Application::getInstance();
    app->initGLContextAttrs();
    cocos2d::GLViewImpl::convertAttrs();

    // Override point for customization after application launch.

    // Add the view controller’s view to the window and display.
    window = initWithFrame: bounds]];

    // Init the CCEAGLView
    CCEAGLView eaglView =
    pixelFormat: (NSString
    )cocos2d::GLViewImpl::_pixelFormat
    depthFormat: cocos2d::GLViewImpl::_depthFormat
    preserveBackbuffer: NO
    sharegroup: nil
    multiSampling: NO
    numberOfSamples: 0 ];

    // Enable or disable multiple touches
    ;

    // Use RootViewController manage CCEAGLView
    _viewController = initWithNibName:nil bundle:nil];
    _viewController.wantsFullScreenLayout = YES;
    _viewController.view = eaglView;

    //add ad start
    AdController *adCrl = initWithFrame: rootController:_viewController];
    ;
    showAdAtTop];
    //add ad end

    // Set RootViewController to window
    if ( .systemVersion floatValue] < 6.0)
    {
    // warning: addSubView doesn’t work on iOS6
    ;
    }
    else
    {
    // use this method on ios6
    ;
    }

    ;

    setStatusBarHidden:true];

    // IMPORTANT: Setting the GLView should be done after creating the RootViewController
    cocos2d::GLView *glview = cocos2d::GLViewImpl::createWithEAGLView(eaglView);
    cocos2d::Director::getInstance()->setOpenGLView(glview);

    app->run();

    return YES;
    }

然后写一个桥接类,方便c++代码控制oc代码:
#ifndef AdControllerBridage_hpp
#define AdControllerBridage_hpp

#include
class AdControllerBridage{

public:
static void showAdAtTop();
static void showAdAtBottom();
};

#endif /* AdControllerBridage_hpp */

#include “AdControllerBridage.h”
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#import “AdController.h”
#endif

void AdControllerBridage::showAdAtTop(){

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
showAdAtTop];
#endif

}
void AdControllerBridage::showAdAtBottom(){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
showAdAtBottom];
#endif
}

最后是在c++代码中调用oc控制方法:

void MapScreen::adControll()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
JniMethodInfo minfo;
bool isHave = JniHelper::getStaticMethodInfo(minfo,
“org/cocos2dx/cpp/AppActivity”, “showBannerStatic”,"(I)V");

if (isHave)
{
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, 0);
}
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
AdControllerBridage::showAdAtBottom();
#endif

}

水平有限,有高招的大家再讨论

1赞