Last active
December 2, 2018 12:31
-
-
Save cabdesigns/4750323 to your computer and use it in GitHub Desktop.
Integrating AdMob into your Apache Cordova project. Implementation positions ad unit at bottom of window and ensures it doesn't overlay and block the WebView.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 { | |
GADBannerView *bannerView_; | |
} | |
@end | |
@interface MainCommandDelegate : CDVCommandDelegateImpl | |
@end | |
@interface MainCommandQueue : CDVCommandQueue | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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 "MainViewController.h" | |
@implementation MainViewController | |
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil | |
{ | |
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; | |
if (self) { | |
// Uncomment to override the CDVCommandDelegateImpl used | |
// _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self]; | |
// Uncomment to override the CDVCommandQueue used | |
// _commandQueue = [[MainCommandQueue alloc] initWithViewController:self]; | |
} | |
return self; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
// Uncomment to override the CDVCommandDelegateImpl used | |
// _commandDelegate = [[MainCommandDelegate alloc] initWithViewController:self]; | |
// Uncomment to override the CDVCommandQueue used | |
// _commandQueue = [[MainCommandQueue alloc] initWithViewController:self]; | |
} | |
return self; | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
// Releases the view if it doesn't have a superview. | |
[super didReceiveMemoryWarning]; | |
// Release any cached data, images, etc that aren't in use. | |
} | |
#pragma mark View lifecycle | |
- (void)viewWillAppear:(BOOL)animated | |
{ | |
// View defaults to full size. If you want to customize the view's size, or its subviews (e.g. webView), | |
// you can do so here. | |
[super viewWillAppear:animated]; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
// Do any additional setup after loading the view from its nib. | |
} | |
- (void)viewDidUnload | |
{ | |
[super viewDidUnload]; | |
// Release any retained subviews of the main view. | |
// e.g. self.myOutlet = nil; | |
} | |
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation | |
{ | |
// Return YES for supported orientations | |
return [super shouldAutorotateToInterfaceOrientation:interfaceOrientation]; | |
} | |
/* Comment out the block below to over-ride */ | |
/* | |
- (UIWebView*) newCordovaViewWithFrame:(CGRect)bounds | |
{ | |
return[super newCordovaViewWithFrame:bounds]; | |
} | |
*/ | |
#pragma mark UIWebDelegate implementation | |
- (void)webViewDidFinishLoad:(UIWebView*)theWebView | |
{ | |
// Black base color for background matches the native apps | |
self.view.backgroundColor = [UIColor blackColor]; | |
theWebView.backgroundColor = [UIColor blackColor]; | |
// Resize frame height to ensure it doesn't get overlapped by ad unit | |
CGRect viewBounds = CGRectMake([[UIScreen mainScreen] applicationFrame].origin.x, [[UIScreen mainScreen] applicationFrame].origin.y-20, [[UIScreen mainScreen] applicationFrame].size.width, [[UIScreen mainScreen] applicationFrame].size.height-CGSizeFromGADAdSize(kGADAdSizeBanner).height); | |
theWebView.frame = viewBounds; | |
// Position ad unit after the web view | |
CGPoint origin = CGPointMake(0.0,theWebView.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 = @"___ADMOBID___"; | |
// 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]]; | |
return [super webViewDidFinishLoad:theWebView]; | |
} | |
/* Comment out the block below to over-ride */ | |
/* | |
- (void) webViewDidStartLoad:(UIWebView*)theWebView | |
{ | |
return [super webViewDidStartLoad:theWebView]; | |
} | |
- (void) webView:(UIWebView*)theWebView didFailLoadWithError:(NSError*)error | |
{ | |
return [super webView:theWebView didFailLoadWithError:error]; | |
} | |
- (BOOL) webView:(UIWebView*)theWebView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType | |
{ | |
return [super webView:theWebView shouldStartLoadWithRequest:request navigationType:navigationType]; | |
} | |
*/ | |
@end | |
@implementation MainCommandDelegate | |
/* To override the methods, uncomment the line in the init function(s) | |
in MainViewController.m | |
*/ | |
#pragma mark CDVCommandDelegate implementation | |
- (id)getCommandInstance:(NSString*)className | |
{ | |
return [super getCommandInstance:className]; | |
} | |
/* | |
NOTE: this will only inspect execute calls coming explicitly from native plugins, | |
not the commandQueue (from JavaScript). To see execute calls from JavaScript, see | |
MainCommandQueue below | |
*/ | |
- (BOOL)execute:(CDVInvokedUrlCommand*)command | |
{ | |
return [super execute:command]; | |
} | |
- (NSString*)pathForResource:(NSString*)resourcepath; | |
{ | |
return [super pathForResource:resourcepath]; | |
} | |
@end | |
@implementation MainCommandQueue | |
/* To override, uncomment the line in the init function(s) | |
in MainViewController.m | |
*/ | |
- (BOOL)execute:(CDVInvokedUrlCommand*)command | |
{ | |
return [super execute:command]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updated to fix issue with ads not being clickable.