Hi guys,
Here is the AdMob module I made based on kam187’s work. It is similar in use to my InMobi helper module if you tried it.
This allows for easy switching between AdMob in InMobi in your game:
[lua]require “admob”
require “inmobi”
require “settings”
– assuming “isFreeVersion” and “isAdMob” are defined in the “settings” module
if settings.isFreeVersion then
if settings.isAdMob then
admob.showAd(true)
else
inmobi.showAd(1, 0, 0)
end
end[/lua]
The setup is simple. Just create a file named “admob.lua”:
[lua]module(…, package.seeall)
local dummyAd
local isAdVisible = false
local isAdOnTop = false
local dummyTimer
local isAndroid = “Android” == system.getInfo(“platformName”)
local CW = display.contentWidth
local CH = display.contentHeight
local SOX = display.screenOriginX
local SOY = display.screenOriginY
local function round(n)
return math.floor(n + 0.5)
end
local function adToFront()
if dummyAd then
dummyAd:toFront()
end
end
local function showAd_Android(event)
– Is the url a remote call?
if string.find(event.url, “android_ad.html”, 1, false) then
return true
else
system.openURL( string.gsub(event.url, “Corona:”, “”) )
– Refresh ad
hideAd()
showAd(isAdOnTop)
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
– an actual click on an ad, so open in Safari
system.openURL(event.url)
– Refresh ad
hideAd()
showAd(isAdOnTop)
end
else
return true
end
end
function showAd(onTop)
if isAdVisible then
if (isAdOnTop == onTop) then
return
else
hideAd()
end
end
isAdOnTop = onTop == true
hideAd()
local adfile = “apple_ad.html”
local sizeX = 320
local sizeY = 48
local posX = (CW - sizeX) / 2
– round the Y position to remove the 1px gap
– between the bottom of the ad the bottom of the screen
local posY = isAdOnTop and SOY or round(CH - SOY - sizeY)
if isAndroid then
adfile = “android_ad.html”
native.showWebPopup(posX, posY, sizeX, sizeY, adfile,
{
baseUrl = system.ResourceDirectory,
hasBackground = false,
urlRequest = showAd_Android,
autoCancel = false
}
)
elseif system.getInfo(“environment”) == “simulator” then
dummyAd = display.newRect(posX, posY, sizeX, sizeY)
dummyTimer = timer.performWithDelay(1000, adToFront, 0)
else
native.showWebPopup(posX, posY, sizeX, sizeY, adfile,
{
baseUrl = system.ResourceDirectory,
hasBackground = false,
urlRequest = showAd_Apple,
autoCancel = false
}
)
end
isAdVisible = true
end
function hideAd()
native.cancelWebPopup()
if dummyTimer then
timer.cancel(dummyTimer)
dummyTimer = nil
end
if dummyAd then
dummyAd:removeSelf()
dummyAd = nil
end
isAdVisible = false
end[/lua]
and an HTML file named “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: 'YOUR ID HERE!!!', // publisher id<br> bgcolor: '356FA8', // 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>
<script type="text/javascript"><br> _admob.gotourl = function (V, W) {<br> document.location = "Corona:" + V;<br> return true;<br> };<br> </script>
[/html]
NOTE: I haven’t tested this on the iOS, but it will probably work there too.
“apple_ad.html” code:
[code]
br> “http://www.w3.org/TR/html4/loose.dtd”>
[/html]
[/code] [import]uid: 52103 topic_id: 16514 reply_id: 316514[/import]

[import]uid: 52103 topic_id: 16514 reply_id: 73197[/import]