Skip to content

Instantly share code, notes, and snippets.

@appdimensions
Last active December 23, 2015 06:39
Show Gist options
  • Save appdimensions/6595555 to your computer and use it in GitHub Desktop.
Save appdimensions/6595555 to your computer and use it in GitHub Desktop.
AdMob for PhoneGap iOS (partially tested 2.9.0). Works for any screen orientation. Can manually show or hide ads. Uses Banner ad size. Thanks to Chris Bell (https://gist.github.com/cabdesigns/4750323) from which this code is based. For Smart Banner ad size, see: https://gist.github.com/appdimensions/6563436
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
//
// MainViewController.h
// ___YOURPROJECT___
//
// Created by ___FULLUSERNAME___ on ___DATE___.
// Copyright ___ORGANIZATIONNAME___ ___YEAR___. All rights reserved.
//
#import <Cordova/CDVViewController.h>
#import <Cordova/CDVCommandDelegateImpl.h>
#import <Cordova/CDVCommandQueue.h>
#import "GADBannerView.h"
@interface MainViewController : CDVViewController <GADBannerViewDelegate>{
BOOL adLoaded;
GADBannerView *bannerView_;
}
@end
@interface MainCommandDelegate : CDVCommandDelegateImpl
@end
@interface MainCommandQueue : CDVCommandQueue
@end
/*
INSTRUCTIONS:
1. Initialize "adLoaded = false;" i.e. within the method initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
2. Then ADD the following code to MainViewController.m
3. Insert your AdMob ID where you see "__ADMOB_ID__" in the code.
4. Ads will not appear by default. To control ad visiblity: "[self showAds];" or "[self hideAds];"
*/
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
if(adLoaded)
[self setAdSize];
}
-(void) setAdSize{
CGRect viewBounds = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-CGSizeFromGADAdSize(kGADAdSizeBanner).height);
self.webView.frame = viewBounds;
[bannerView_ setAdSize:kGADAdSizeBanner];
[bannerView_ setFrame:CGRectMake(0, self.webView.frame.size.height, bannerView_.frame.size.width, bannerView_.frame.size.height)];
}
-(void)adViewDidReceiveAd:(GADBannerView *)bannerView{
if(!adLoaded){
[self setAdSize];
adLoaded = true;
}
}
-(void)adView:(GADBannerView*)banner didFailToReceiveAdWithError:(GADRequestError*)error
{
CGRect viewBounds = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
if(!CGRectEqualToRect(viewBounds, self.webView.frame)){
self.webView.frame = viewBounds;
adLoaded = false;
[bannerView_ setFrame:CGRectMake(0, self.webView.frame.size.height, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
}
adLoaded = false;
}
// SHOW THE ADS (call this method when you want to show the ads, or add the code in the method "webViewDidFinishLoad:(UIWebView*)theWebView" to show the ads immediately //
-(void)showAds{
CGRect viewBounds = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - CGSizeFromGADAdSize(kGADAdSizeBanner).height);
if(!CGRectEqualToRect(viewBounds, self.webView.frame)){
self.webView.frame = viewBounds;
// Position ad unit after the web view
CGPoint origin = CGPointMake(0.0,self.webView.frame.size.height);
// Create a view of the standard size at the top of the screen.
// Available AdSize constants are explained in GADAdSize.h.
bannerView_ = [[[GADBannerView alloc] initWithAdSize: kGADAdSizeBanner origin:origin] autorelease];
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = @"__ADMOB_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_];
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest: [GADRequest request]];
[bannerView_ setDelegate: self];
adLoaded = true;
}
}
// HIDE THE ADS //
-(void)hideAds{
CGRect viewBounds = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMinY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds));
if(!CGRectEqualToRect(viewBounds, self.webView.frame)){
self.webView.frame = viewBounds;
[bannerView_ removeFromSuperview];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment