Here is a modified version, compatible with recent releases of Corona and made as a module - name it whatever you want, but don’t name it “ads.lua” since this is now reserved by Corona for InMobi and future ad providers. For example, “admob.lua” will do.
I also added the option to show the ad on top of the screen.
However, it looks like the timer workaround for jumping ads in original code doesn’t work anymore. When createAd() is call the ad will display normally, then it will quickly shift 2px to the left, then 1px to the right and again 1px to the right, returning to it’s normal position. This happens quickly in 2 sec or so and is a minor thing, but if somebody knows how to get rid of this jumping behavior please share a solution here.
[lua]module(…, package.seeall)
local dummy
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 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
removeAd()
createAd()
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
removeAd()
createAd()
end
else
return true
end
end
function createAd(showOnTop)
native.cancelWebPopup()
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 = showOnTop 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
}
)
elseif system.getInfo(“environment”) == “simulator” then
dummy = display.newRect(posX, posY, sizeX, sizeY)
display.getCurrentStage():insert(dummy, false)
else
native.showWebPopup(posX, posY, sizeX, sizeY, adfile,
{
baseUrl = system.ResourceDirectory,
hasBackground = false,
urlRequest = showAd_Apple
}
)
end
end
function removeAd()
native.cancelWebPopup()
if dummy then
dummy:removeSelf()
dummy = nil
end
end[/lua] [import]uid: 52103 topic_id: 8021 reply_id: 53535[/import]