Admobs and Monkey iOS guide

Monkey Targets Forums/iOS/Admobs and Monkey iOS guide

pantson(Posted 2011) [#1]
Heres a quick guide to get Admob ads working in your monkey app.

This guide will display the ads at the bottom of a portrait screen.

This guide is based on the iOS guide from google here
http://code.google.com/mobile/ads/docs/ios

First, set up an iOS ad in Admob..


Second, open up your latest compiled project in xcode and add your project


Thirdly, add the extra libs to your project in xcode


Now, its time to alter the main.h and main.mm files.
Open up main.h and add the create ad banner code to the main view
// ***** MonkeyViewController *****

@interface MonkeyViewController : UIViewController{
  GADBannerView *bannerView_;
@public
}


Next open up main.mm and add the import line for the ad lib
#import "GADBannerView.h"


next add the following to the top of @implementation MonkeyViewController
- (void)viewDidLoad {
  [super viewDidLoad];

  // Create a view of the standard size at the bottom of the screen.
  bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,
		[[UIScreen mainScreen] bounds].size.height - GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];

  // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
  bannerView_.adUnitID = @"UNIT_ID";

  // 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_];

  // test devivce
	GADRequest *request = [GADRequest request];
	
	request.testDevices = [NSArray arrayWithObjects:
			GAD_SIMULATOR_ID,                               // Simulator
			nil];  
	
	// Initiate a generic request to load it with an ad.
  [bannerView_ loadRequest:request];
	
	
}

- (void)viewDidUnload {
  [bannerView_ release];
}

- (void)dealloc {
  [super dealloc];
}

replacing UNIT_ID with your advert unit ID

That should be it.. recompile and a test advert should appear in the simulator.

I don't know what happens if you upload to itunes and download to a real device. you may have to remove the following code
	request.testDevices = [NSArray arrayWithObjects:
						   GAD_SIMULATOR_ID,                               // Simulator
						   nil];  



MikeHart(Posted 2011) [#2]
Thanks a lot man.


Qcat(Posted 2011) [#3]
Thanks! looks good will have to try it out when i get time


Neuro(Posted 2011) [#4]
Cool, this should also go into the Tutorial section.


Dabz(Posted 2011) [#5]
Got this working!!! YAY!!! :D

Dabz


samowitsch(Posted 2012) [#6]
I have added a few lines where the position and the rotation of the banner can be defined.

Take a look at:
+ the object makelandscape
+ bannerView_.transform

This example is inspired from this tutorial where a admob banner is placed centered at the top within a iphone landscape game.
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Create a view of the standard size at the bottom of the screen.
    bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(([[UIScreen mainScreen] bounds].size.width / 2) - (GAD_SIZE_320x50.width / 2),
                                            [[UIScreen mainScreen] bounds].size.height - GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];
    
    //Rotation of the AdMob Banner, change 0.0f for example to 0.5f
    CGAffineTransform makeLandscape = CGAffineTransformMakeRotation(M_PI * 0.0f);
    
    //and Position of the Banner, the values depends to the values of the CGRectMake of the bannerView_ object
    makeLandscape = CGAffineTransformTranslate(makeLandscape, 0, -400);
    bannerView_.transform = makeLandscape;

    
    // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
    bannerView_.adUnitID = @"UNIT_ID";
    
    // 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_];
    
    // test devivce
	GADRequest *request = [GADRequest request];
	
	request.testDevices = [NSArray arrayWithObjects:
                           GAD_SIMULATOR_ID,                               // Simulator
                           nil];  
	
	// Initiate a generic request to load it with an ad.
    [bannerView_ loadRequest:request];	
}



Xaron(Posted 2012) [#7]
Great, thanks for that! :)


pantson(Posted 2012) [#8]
Can this modulised at all?
In this post I did an Admob module for Android (not perfect)
http://www.monkeycoder.co.nz/Community/posts.php?topic=2185


CopperCircle(Posted 2012) [#9]
Just tried this but getting unknown type name 'GADBannerView'? Followed the tutorial and made sure the #import "GADBannerView.h" is in main.mm, any ideas?

Thanks.


York(Posted 2012) [#10]
Import the "GADBannerView.h" in main.h


CopperCircle(Posted 2012) [#11]
Thanks, I did already try that but get a different error:

Undefined symbols for architecture i386:
"CGSizeFromGADAdSize(GADAdSize)", referenced from:
-[MonkeyViewController viewDidLoad] in main.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)


benmc(Posted 2012) [#12]
Something has changed in the latest AdMob SDK. I get the same linker error. I can use an older version of the SDK and this all works fine. Anyone know what could have changed with Ad sizes?

EDIT: Found the problem. There were some name changes to constants :)

https://developers.google.com/mobile-ads-sdk/docs/ios/intermediate

Change the GAD_ ad sizes with:

kGADAdSizeBanner.size.width and kGADAdSizeBanner.size.height


benmc(Posted 2012) [#13]
Does anyone have any idea how I might go about hiding and showing an ad during the game? So I don't have to show it when the game is running, but just on game over screens?

I've marked out the locations in my main.mm where I need to show and hide the bannerView_ but I can't figure out how to reference that view from the CPP to do a bannerView_.hidden = YES; and bannerView_.hidden = NO;

Any help would be appreciated.


silentshark(Posted 2012) [#14]
Did you have any luck on the hiding/ showing ads, benmc?

I've just been reading thru the above and it looks a lot more frightening without a nice module to hook into (like weve got for android now). Maybe it's not difficult, and I'm just too much of an iOS noob :-)


benmc(Posted 2012) [#15]
No, I could not figure it out unfortunately. I'm bummed too because our new game really can't go on iOS until I get this squared away.


silentshark(Posted 2012) [#16]
Rats, this would be really handy. I'm sure there must be a way to do this..

Maybe the answer lies deep in the google groups forum - https://groups.google.com/forum/?fromgroups#!forum/google-admob-ads-sdk - but I can't find it!

Any help would be appreciated :-)


silentshark(Posted 2012) [#17]
@benmc, you hit the nail on the head - seems there's a scoping issue that stops you referencing BannerView_ from the main monkey produced C++ code. Shame, and I'm sure there's an easy answer, but I'm out of my depth.

Any iOS gurus out there who can help? (Pretty please!)


York(Posted 2012) [#18]
Hey,

Anybody knows, if the ad refreshs while _bannerView.hidden = TRUE; is set? And the auto-refresh is activated in admob.. ?


Now here´s a solution:

You need 2 more methods in MonkeyViewController to hide and show the ad..

@interface MonkeyViewController : UIViewController{
  GADBannerView *bannerView_;
@public
- (void) hideads;
- (void) showads;
}



In main.mm you add to your code ( MonkeyViewController ):

- (void)viewDidLoad {
  [super viewDidLoad];

  // Create a view of the standard size at the bottom of the screen.
  bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,
		[[UIScreen mainScreen] bounds].size.height - GAD_SIZE_320x50.height,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];

  // Specify the ad's "unit identifier." This is your AdMob Publisher ID.
  bannerView_.adUnitID = @"UNIT_ID";

  // 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_];

  // test devivce
	GADRequest *request = [GADRequest request];
	
	request.testDevices = [NSArray arrayWithObjects:
			GAD_SIMULATOR_ID,                               // Simulator
			nil];  
	
	// Initiate a generic request to load it with an ad.
  [bannerView_ loadRequest:request];
	
   bannerView.hidden = TRUE;	
}

- (void)hideads {
    _bannerView.hidden = TRUE;
}

- (void) showads {
    _bannerView.hidden = FALSE;
}

- (void)viewDidUnload {
  [bannerView_ release];
}

- (void)dealloc {
  [super dealloc];
}




If you now want to call the hide or show method you must only call this in your code:

    [app->appDelegate->viewController showads];


or

     [app->appDelegate->viewController hideads];



silentshark(Posted 2012) [#19]
Hey York! many thanks for this.. I will give it a whirl. I owe you a beer :-)


silentshark(Posted 2012) [#20]
Well, I got that working, but needed to tweak York`s code slightly. In the main.h file, I needed to declare thus, otherwise Xcode gave me an error.

@interface MonkeyViewController : UIViewController{
    GADBannerView *bannerView_;
}

    - (void) hideads;
    - (void) showads;

@end


..and in the main.mm files, I needed the functions to appear thus:

- (void)hideads {
    bannerView_.hidden = TRUE;
}

- (void) showads {
    bannerView_.hidden = FALSE;
}


(the variable should be bannerView_, not _bannerView.)

Then it compiled ok, and calling the functions as York suggested seemed to work just fine - ads being hidden and reappearing on demand.. Happy days.


benmc(Posted 2012) [#21]
I'd like to report that the hiding and showing of ads works perfectly!! This opens a lot of doors for me, I'm very excited, thanks for everyone's help.


Lugato(Posted 2012) [#22]
Hi to all,

I'm given a error when I compile my app:

Undefined symbols for architecture i386:
"CGSizeFromGADAdSize(GADAdSize)", referenced from:
-[MonkeyViewController viewDidLoad] in main.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Can anyone give a little help here ?

thnks


benmc(Posted 2012) [#23]
@Lugato - The first thing to check is to make sure the most recent SDK/lib is actually being included in a fresh build.

Second, up above in this thread, someone else pasted the exact same error, and I was getting it too. I think the resolution to your problem is here:

Changes to the function/variable names:
https://developers.google.com/mobile-ads-sdk/docs/ios/intermediate

Change the GAD_ ad sizes with:
kGADAdSizeBanner.size.width and kGADAdSizeBanner.size.height


silentshark(Posted 2012) [#24]
Did anyone figure out the bit about GAD_SIMULATOR_ID, and whether the following line should be removed from production builds?

	request.testDevices = [NSArray arrayWithObjects:
			GAD_SIMULATOR_ID,                               // Simulator
			nil];  


Update - I've done some experimenting, and some googling. I'm unsure of the definitive position, but it looks like the above line does not hurt, so I'm leaving it in for now.. Cheers all


Xaron(Posted 2012) [#25]
Has something changed lately?

I get a signal SIGABRT in the main function call in this line:

UIApplicationMain( argc,argv,nil,nil );


...using the latest Xcode and iOS and Google Admob SDK.


Xaron(Posted 2012) [#26]
Ok, got that crash solved:

https://developers.google.com/mobile-ads-sdk/docs/

4. You now need to add -ObjC to the Other Linker Flags of your application target's build setting:


But I don't see any ads in the simulator now, nor testing neither real ones...


MikeHart(Posted 2012) [#27]
It takes sometimes a few hours till they show up.


Xaron(Posted 2012) [#28]
Thanks Mike, but no, that's not the problem...


frank(Posted 2013) [#29]
Anyone solved this? Also is there any way this can be a module ? Because it's annoying to re-add the code all the time?

Is there another ad framework which is a module which I can use ? Preferably on iOS + Android?

Thanks!


Shinkiro1(Posted 2013) [#30]
Admob comes bundled with monkey since v67 :)


frank(Posted 2013) [#31]
I'm using Monkey v70 for admob in iOS; a) it doesn't compile with admob; it copies the header files to the libs directory in the project where xcode cannot find them => actually it tries to copy them or something; they appear in red in the project. copying them in manually to that libs directory makes them (still) unfindable b) iOS, unlike Android, seems to have no test settings, which is bad for, well, testing :)

To resolve the issues;

1) I add the files manually to the project (Add files to project) as described by Google ; compilation succeeds after that
2) I add testDevices manually to the request which works as well => it compiles , it doesn't actually work

Both are a hassle ofcourse...

Edit: did anyone get this running at all on iOS? It keeps nagging that I need to add my simulator ID to testDevices which I did. Doesn't work on iPad either; same message.

I found 1 person on Google with the same issue for which the answer was that he didn't properly attached the view. But I guess here we are, so i'm kind of at a loss as to how to get this working...

Edit: If I add them to the BBMonkeyViewController they work fine... So it's definitely something wrong with the brl module.