SOLVED: Adding AdMob Ads Natively (no plugins required) for iOS

1,108 views
Skip to first unread message

Keyvisuals

unread,
Sep 26, 2011, 10:52:14 PM9/26/11
to phonegap
I want to share this with the group because it seems that these
directions don't exist ANYWHERE. I'm not really sure what the big
secret is but I want to de-mystify it and hopefully save some others
precious time. It's really simple and Does Not involve installing any
phonegap plugins ;)

1.) Create your normal, everyday phonegap (1.0.0) project in xcode
2.) Import the GoogleAdMobAdsSDK folder and the required AdMob
frameworks (I believe the only one that wasn't already in the phonegap
project was MessageUI.framework)
3.) In AppDelegate.h - #import "GADBannerView.h" & add GADBannerView
*bannerView_; to your @interface
4.) In AppDelegate.m - #define MY_BANNER_UNIT_ID @"Your AdMob
Publisher ID# Here"

Now the tricky part. This caused me to bang my head on the wall for
some time . . .

5.) Change your method webViewDidFinishLoad to this . . .

- (void)webViewDidFinishLoad:(UIWebView *)theWebView
{
bannerView_ = [[GADBannerView alloc]init];
[bannerView_ setDelegate:self];
[bannerView_ setFrame:CGRectMake(0, 430, 320, 50)];

// Specify the ad's "unit identifier." This is your AdMob
Publisher ID.
bannerView_.adUnitID = MY_BANNER_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.viewController;
[self.viewController.view addSubview:bannerView_];

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

// only valid if AdGap.plist specifies a protocol to handle
if(self.invokeString)
{
// this is passed before the deviceready event is fired, so you can
access it in js when you receive deviceready
NSString* jsString = [NSString stringWithFormat:@"var invokeString =
\"%@\";", self.invokeString];
[theWebView stringByEvaluatingJavaScriptFromString:jsString];
}
return [ super webViewDidFinishLoad:theWebView ];
}


That's it.

I have created a sample project that does exactly this with the option
to display test ads.
You can download the project from http://iphone.keyvisuals.com/downloads/AdGap.zip

Why does the banner view need to be created in the
webViewDidFinishLoad method? I think it's because phonegap doesn't
create the viewController until after the webview but I might be
wrong. This might not be the ideal solution for everyone's app but it
works for me. Enjoy.

Keyvisuals

unread,
Sep 26, 2011, 10:54:30 PM9/26/11
to phonegap
BTW - I'm not affiliated in any way with AdMob.

trek7k

unread,
Sep 27, 2011, 3:48:52 PM9/27/11
to phonegap
Perfect, exactly what I was looking for!

I do get a warning on the [bannerView_ setDelegate:self]; line:

Class 'AppDelegate' does not implement the 'GADBannerViewDelegate'
protocol

Are you getting the same message? Does this matter?

Keyvisuals

unread,
Sep 27, 2011, 8:50:33 PM9/27/11
to phonegap

Yeah, it does matter. Seems I left off a step.

Call it 3b.) Make sure you implement <GADBannerViewDelegate> in your
AppDelegate.h

Giacomo Balli

unread,
Oct 2, 2011, 3:24:11 AM10/2/11
to phonegap
Hey there, was wondering about some numbers regarding AdMob.
Would you mind sharing your experience with ecpm and fill rate?

thank you!

Marcus

unread,
Oct 3, 2011, 11:47:48 PM10/3/11
to phon...@googlegroups.com
Is there anything wrong with using AdMob's javascript code for websites?
I tried that and the test ad appears.

Marcus

Derrick

unread,
Oct 25, 2011, 1:06:02 PM10/25/11
to phonegap
I am trying to debug the javascript one but you need to turn off test.
In your code set test equal to false
Reply all
Reply to author
Forward
0 new messages