Final Solution to integrating AdMob into Android
Many others have struggled with integrating AdMob into your Android apps and I did as well. I spent many hours trying every suggested solution, from manipulating the returned web view html to fire off ti events to plugging in ad jar files. Every solution failed for different reasons, some because they didn't work with the latest titanium sdk version, and others because they assumed that the web view ad html would contain links when in reality the production admob ads load images into iframes.
All of this being said, here is a solution that actually works and is really simple.
- First you make a public web page that contains your ad mob code.
- Then you can load it into a webview. The trick with webview is that it loads twice. Once to load the ad, then a second time when you click the ad.
- When clicking the ad, the evt.url property of the load event will be equal to the ad destination url so you can capture that and launch it off in a new browser.
- If you stop there then if they return to your app, it will show the ad destination loaded in your webview. For that reason, you also want to reload the ad webview to contain a new ad.
var console = { log: Ti.API.info };
var adUrl = "http://tothsolutions.com/ad/mobile";
var adWebView = null;
displayAd();
function displayAd() {
var numLoads = 0;
if (adWebView) {
win.remove(adWebView);
}
adWebView = Ti.UI.createWebView({ url: adUrl, height: 48, bottom: 0, left: 0, right: 0 });
win.add(adWebView);
adWebView.addEventListener('load', function (evt) {
numLoads++;
if (numLoads > 1) {
var url = evt.url.split('market://').join('http://');
console.log("clicked ad " + url);
//launch ad url in new browser window.
Ti.Platform.openURL(url);
//load a new ad.
displayAd();
}
});
}
Much props to my brother Dawson for figuring out the trick with the webview load event!
11 Answers
-
hi Justin,
i tried it and it worked!
question abt the public web page used though: does admob approve of using a public web page to serve ads? Each time a user clicks on it, the admob managers will see that the referrer originates from the web page, not the app.
anyone has any experience on this end?
-
Hi Justin,
How can i make a public web page that contains ad mob code. -
I'm a little new to Titanium but couldn't you just put the code in an html file included in your app project and call it from there instead of an external website? I'm at work atm and can't try it personally but you would think it would work.
If it does work, it would most likely load faster too as you would not be dependent on your webserver to feed the html file holding the ad. Just a thought….
Joe
-
Hi Justin,
In admob i create a "mobile site" ad, then they gave me php code. I paste this php code into a new web page on my web server, but
the publisher code is set to test mode. When i make request to server in this mode,It return the "AdMob Test Ad".But when i set the "TEST_MODE" variable to "false",it does not return anything. -
Thank you for sharing the details of this solution, Justin.
I'm having difficulty getting this to work. Upon creating a new site on admob, I don't see an option for "mobile site". If I select "mobile web", and view the code to publish it, there isn't an option for Javascript - only PHP, C#, VB.net…
Upon selecting "Smartphone Web", this does present Javascript code to publish, but this is for iphone only, and I'm trying to deploy on Android. I created a .net version and deployed on a site, but when accessing the .aspx from my android phone browser the screen was blank. Acessing from my PC browser displayed the test message.
I also tried to use the PHP script, posting on my website and accessing the same way, and had the same results - test ad shows in PC browser but blank screen when rendered on my phone.
Any help greatly appreciated …
-
Does this works for android?
-
I get an error using this code it give me:
Wrapped java.lang.NullPointerException (file://android_asset/Resources/app.js#28and this is my code
/////// NO BORRAR
var webview = Titanium.UI.createWebView({url:'/html/index.html'});
var window = Titanium.UI.createWindow();
window.add(webview);/////// ADS
var console = { log: Ti.API.info };
var adUrl = "THIS IS WHERE THE HTML AD WILL BE I HAVE REMOVE IT";
var adWebView = null;displayAd();
function displayAd() {
var numLoads = 0;
if (adWebView) {
window.remove(adWebView);
}
adWebView = Ti.UI.createWebView({ url: adUrl, height: 48, bottom: 0, left: 0, right: 0 });
window.add(adWebView);
adWebView.addEventListener('load', function (evt) {
numLoads++;
if (numLoads > 1) {
var url = evt.url.split('market://').join('http://');console.log("clicked ad " + url); //launch ad url in new browser window. Ti.Platform.openURL(url); //load a new ad. displayAd(); } });
}
/////// NO BORRAR
window.open({modal:true});
-
I get an error using this code it give me:
Wrapped java.lang.NullPointerException (file://android_asset/Resources/app.js#28and this is my code
/////// NO BORRAR
var webview = Titanium.UI.createWebView({url:'/html/index.html'});
var window = Titanium.UI.createWindow();
window.add(webview);/////// ADS
var console = { log: Ti.API.info };
var adUrl = "THIS IS WHERE THE HTML AD WILL BE I HAVE REMOVE IT";
var adWebView = null;displayAd();
function displayAd() {
var numLoads = 0;
if (adWebView) {
window.remove(adWebView);
}
adWebView = Ti.UI.createWebView({ url: adUrl, height: 48, bottom: 0, left: 0, right: 0 });
window.add(adWebView);
adWebView.addEventListener('load', function (evt) {
numLoads++;
if (numLoads > 1) {
var url = evt.url.split('market://').join('http://');console.log("clicked ad " + url); //launch ad url in new browser window. Ti.Platform.openURL(url); //load a new ad. displayAd(); } });
}
/////// NO BORRAR
window.open({modal:true});
-
This Approach must have an own web server, If I haven't own web server. How can I do, Have any suggestion for me?
Note:I have been successful integrate the iPhone App with AdMob on "Smartphone Web" approach.
Now I want to try deployed it to Android Device. I can use Titanium "createWebView" method to
present the AdMob banner, but If the user clicked the AD banner, I can't detect this event.
My purpose is wanting let the click AdMob Banner can open on New Window.Ref1: Admob for iPhone
http://developer.admob.com/wiki/IPhone- This document described, I can use "new_windos: true" to control it.
Ref2: Admob for Android
http://developer.admob.com/wiki/Android- But on the Android hasn't the "new_windos: true" method.
Does anyone have any solution on it? I want to detect the "Click AdMob Banner" behavior to open on new window.
Thank you!
-
I was about to use this solution on a project I'm working on until I saw Clifford Tan's post that his account got suspended for this. Not sure if you guys saw this other solution provided on http://developer.appcelerator.com/question/39161/ad-layers. I got it working in the little sample app Ti generates. Haven't gotten it up and running in one of my apps yet. It was a little confusing to figure out at first because his directions are for linux. I can explain how i did it on windows 7.
Download his zip: http://CrucialDivide.com/mobilesdk-dasher-ads-1.4-linux.zip and then find where your titanium sdk stuff is. For me i had to display hidden folders and it was at C:\ProgramData\Titanium\mobilesdk.
Inside the zip file open mobilesdk/linux and copy that dasher-ads-1.4 folder into the folder on your machine(mine was C:\ProgramData\Titanium\mobilesdk\win32) along with the sdks for other releases of Ti mobile. Restart Ti and start a new project and it should pop up as an available sdk. Then refer to this page https://gist.github.com/439986 for implementation. Hope this helps.
-
I apologize for posting this in several threads, but in case anyone is searching for an Android AdMob solution, I have a (simple) beta version of an android module here: https://github.com/bkurzius/Titanium-Android-AdMob-Module.
I posted more info about it here: http://developer.appcelerator.com/question/119673/admob-for-android. Any android devs, please feel free to help improve (or rebuild) it!