EDIT: Added HoneyComb UA fix (check android ad htmls
EDIT: Change made for latest corona.
So, I saw alot of complaints about this and people really strugling with it. I finally found a bit of time looking at it.
I tested this on Corona 243, and daily 317. Other versions may have issues I don’t know.
EDIT: This has developed to a completely working solution on all devices! Instead of leaving the code fragmented across posts I’ve moved EVERYTHING you need to this first post:
Tested on Corona upto latest daily, and also including daily 319 for android.
Step 1)
You MUST add the internet permissions to the build.settings file. For the sake of completness here’s a build.settings file that you can drop in place:
settings = {
orientation =
{
default = "portrait",
supported =
{
"portrait",
},
},
androidPermissions =
{
"android.permission.INTERNET",
"android.permission.ACCESS\_NETWORK\_STATE"
},
}
Step 2) Sign up for admob and create a new ‘site’. Pick Android to get an android id - you’ll use this in step 3. Create a second site and pick ‘iphone’ to get an apple id.
Ignore the sdk download, as you can’t use it with corona. Edit the site and set the refresh rate to 20 seconds (this ensures an ad is refreshed on their side ready for when you http refresh your page).
Step 3) Create the following files from the code below:
main.lua - add this code to your main.lua and use DisplayAd(delay) to show and ad after ‘delay’ milliseconds. Use RemoveAd(delay) to remove it after delay milliseconds.
android_ad.html - This is the html file for showing ads on all android devices with zoom level <= 1.5. Change the admob id to the one you received when you created the '‘site’ in admob.
android_ad15.html - This is the html file for showing ads on all android devices with a zoom level > 1.5. Change the admob id to the one you received when you created the '‘site’ in admob. It should be the same one as above.
apple_ad.html - This handled ALL apple devices. I suggest you create a SECOND site in admob and use a seperate id for this, so you can see how much revenue you are generating from apple vs android.
You should also change the ‘test:’ line to ‘false’ when you want to see real ads.
– main.lua
local adSpace
local isAndroid = "Android" == system.getInfo("platformName")
function showAd\_Android(event)
-- Is the url a remote call?
if string.find(event.url, "android\_ad.html", 1, false) or string.find(event.url, "android\_ad15.html", 1, false) then
return true
else
system.openURL(string.gsub (event.url, "Corona:", ""))
-- Refresh ad
RemoveAd(0)
DisplayAd(0)
return true
end
end
local function showAd\_Apple(event)
-- Is the url a remote call?
if string.find(event.url, "http://", 1, false) == 1 then
-- Is it a call to the admob server?
if string.find(event.url, "c.admob.com", 1, false) == nil then
adSpace.url = event.url
else
-- an actual click on an ad, so open in Safari
system.openURL(event.url)
-- Refresh ad
RemoveAd(0)
DisplayAd(0)
end
else
-- Feb 1, 2011: if using the old version of this code, remove the
-- following line:
-- adSpace.url = event.url
-- and put in this line instead:
return true
end
end
function DisplayAd(t)
native.cancelWebPopup()
timer.performWithDelay(t, function()
local adfile = "apple\_ad.html"
local sizeX = 320
local sizeY = 48
local scale = 1/display.contentScaleY
if isAndroid then
if scale \> 1.5 then
adfile = "android\_ad15.html"
sizeX = sizeX\*(1.5/scale) + 1
sizeY = sizeY\*(1.5/scale) + 1
else
adfile = "android\_ad.html"
end
adSpace = native.showWebPopup((display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY, adfile, {baseUrl = system.ResourceDirectory, hasBackground = false, urlRequest = showAd\_Android})
elseif system.getInfo( "environment" ) == "simulator" then
adSpace = display.newRect( (display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY )
display.getCurrentStage():insert(adSpace, false)
else
if scale \> 1.5 then
sizeX = sizeX\*(1.5/scale) + 1
sizeY = sizeY\*(1.5/scale) + 1
end
adSpace = native.showWebPopup((display.contentWidth - sizeX)/2, display.contentHeight - display.screenOriginY - sizeY, sizeX, sizeY, adfile, {baseUrl = system.ResourceDirectory, hasBackground = false, urlRequest = showAd\_Apple})
end
end
)
end
function RemoveAd(t)
timer.performWithDelay(t, native.cancelWebPopup)
timer.performWithDelay(t, function() adSpace:removeSelf() end)
end
android_ad.html
br\> "http://www.w3.org/TR/html4/loose.dtd"\>
<meta name="viewport" content="width=320; user-scalable=0;">
<meta http-equiv="Refresh" content="30">
<title>ad</title>
<script type="text/javascript"><br> var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> ama: false, // set to true and retain comma for the AdMob Adaptive Ad Unit, a special ad type designed for PC sites accessed from the iPhone. More info: http://developer.admob.com/wiki/IPhone#Web_Integration<br> test: false // test mode, set to false to receive live ads<br> };<br> </script>
<script type="text/javascript"><br> var userAgentTemp = navigator.userAgent;<br> if (!(RegExp("Android (1|2).").test(userAgentTemp))) {<br> navigator = {}<br> if (userAgentTemp.search(/Android/) != -1) {<br> navigator.userAgent = userAgentTemp.replace(/Android 3[\.\d]*/, "Android 2.3.3");<br> } else {<br> navigator.userAgent = userAgentTemp + "; Android 2.3.3";<br> };<br> };<br> </script>
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br> </script>
[/html]
android_ad15.html
br\> "http://www.w3.org/TR/html4/loose.dtd"\>
<meta name="viewport" content="width=320; initial-scale=1.5; minimum-scale=1.5; maximum-scale=1.5; user-scalable=0;">
<meta http-equiv="Refresh" content="30">
<title>ad</title>
<script type="text/javascript"><br> var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> ama: false, // set to true and retain comma for the AdMob Adaptive Ad Unit, a special ad type designed for PC sites accessed from the iPhone. More info: http://developer.admob.com/wiki/IPhone#Web_Integration<br> test: false // test mode, set to false to receive live ads<br> };<br> </script>
<script type="text/javascript"><br> var userAgentTemp = navigator.userAgent;<br> if (!(RegExp("Android (1|2).").test(userAgentTemp))) {<br> navigator = {}<br> if (userAgentTemp.search(/Android/) != -1) {<br> navigator.userAgent = userAgentTemp.replace(/Android 3[\.\d]*/, "Android 2.3.3");<br> } else {<br> navigator.userAgent = userAgentTemp + "; Android 2.3.3";<br> };<br> };<br> </script>
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br> </script>
[/html]
apple_ad.html
br\> "http://www.w3.org/TR/html4/loose.dtd"\>
<meta name="viewport" content="width=320; user-scalable=0;">
<meta http-equiv="Refresh" content="30">
<title>ad</title>
<script type="text/javascript"><br> var admob_vars = {<br> pubid: '000000000000000', // publisher id - FILL IN YOUR OWN ID HERE!!<br> bgcolor: '000000', // background color (hex)<br> text: 'FFFFFF', // font-color (hex)<br> test: false // test mode, set to false to receive live ads<br> };<br> </script>
<script type="text/javascript" src="http://mmv.admob.com/static/iphone/iadmob.js"></script>
[/html]
[import]uid: 8872 topic_id: 8021 reply_id: 308021[/import]