AdMob not show after include SDK and set up in rootcontroller


#1

Sorry to make a separate post, as previous one, it’s for compile issue.

I have included admob and added following code to rootviewcontroller:

in RootViewController.h

 import <UIKit/UIKit.h>
 import "GADBannerView.h"
 @interface RootViewController : UIViewController {
 
 GADBannerView *bannerView_;
 }

@end

in RootViewController.mm:

(void)viewDidLoad {
[super viewDidLoad];

// Create a view of the standard size at the top of the screen.
// Available AdSize constants are explained in GADAdSize.h.
// Create a view of the standard size.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0,366,320,50)];

// Specify the ad unit ID.
bannerView_.adUnitID = @"ca-app-pub-4106+hidemyid+sorry";

// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];

// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:[GADRequest request]];
}

when I run my project, I didn’t see the banner appear.

my project is a cocos2d-x javascript project.

do I have to call the API in my scene javascript files? if so, how to call it?


#2

Add this to AppController.mm

    bannerView_ = [[GADBannerView alloc] initWithAdSize:kGADAdSizeSmartBannerPortrait];
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    CGFloat screenWidth = screenRect.size.width;
    CGFloat screenHeight = screenRect.size.height;
    [bannerView_ setFrame:CGRectMake(0,
                                     screenHeight-bannerView_.bounds.size.height,
                                     bannerView_.bounds.size.width,
                                     bannerView_.bounds.size.height)];
    bannerView_.adUnitID = @"your-ad-unit-id";
    bannerView_.rootViewController = viewController;
    [viewController.view addSubview:bannerView_];
    [bannerView_ loadRequest:[GADRequest request]];

right before

cocos2d::CCApplication::sharedApplication()->run();

Do not modify RootViewController.


#3

Hi sortris, thanks for your kindly help.

I got following error message when use code that you recommended:

2014-02-26 22:18:07.719 project[9441:907] -[GADSlot state]: unrecognized selector sent to instance 0xab86da0
2014-02-26 22:18:07.720 project[9441:907] *** Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[GADSlot state]: unrecognized selector sent to instance 0xab86da0’
*** First throw call stack:
(0x396a012 0x2ee0e7e 0x39f54bd 0x3959bbc 0x395994e 0x90e22e 0x90de8d 0x90cc6e 0x90d262 0x90d591 0x2a36 0x1e22157 0x1e22747 0x1e2394b 0x1e34cb5 0x1e35beb 0x1e27698 0x3e24df9 0x38edf3f 0x38ed96f 0x3910734 0x390ff44 0x390fe1b 0x1e2317a 0x1e24ffc 0x2f1f 0x2725)
libc++abi.dylib: terminate called throwing an exception
(lldb)


#4

don’t worry, I figure out that my other linker flags just changed…should include -ObjC


#5

Thanks very much sortris. This was a huge help!. :slight_smile:


#6

@sortris

do you know how to change the position of the add?

I’ve tried this: http://www.cocos2d-x.org/forums/6/topics/46265

there are no error in console, but didn’t show the ad banner…


#7

in this part of code you have setting the banner position:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
[bannerView_ setFrame:CGRectMake(0,
                                     screenHeight-bannerView_.bounds.size.height,
                                     bannerView_.bounds.size.width,
                                     bannerView_.bounds.size.height)];