-------------------------------------------------------------------------------- -- Sample code is MIT licensed, see http://www.coronalabs.com/links/code/license -- Copyright (C) 2013 Corona Labs Inc. All Rights Reserved. -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- -- Setup -------------------------------------------------------------------------------- -- Hide the status bar display.setStatusBar( display.HiddenStatusBar ) -- Load Corona 'ads' library local ads = require( "ads" ) -------------------------------------------------------------------------------- -- Setup ad provider -------------------------------------------------------------------------------- -- The name of the ad provider. local provider = "admob" -- Your Android application ID local bannerAppID = "ca-app-pub-5387163376908424/8846800273" local interstitialAppID = "ca-app-pub-5387163376908424/8581371075" if system.getInfo("platformName") == "iPhone OS" then -- Your iOS application IDs intersititalAppID = "ca-app-pub-5387163376908424/5741649076 " bannerAppID = "ca-app-pub-5387163376908424/4264915871" end function print\_r ( t ) local print\_r\_cache={} local function sub\_print\_r(t,indent) if (print\_r\_cache[tostring(t)]) then print(indent.."\*"..tostring(t)) else print\_r\_cache[tostring(t)]=true if (type(t)=="table") then for pos,val in pairs(t) do if (type(val)=="table") then print(indent.."["..pos.."] =\> "..tostring(t).." {") sub\_print\_r(val,indent..string.rep(" ",string.len(pos)+8)) print(indent..string.rep(" ",string.len(pos)+6).."}") elseif (type(val)=="string") then print(indent.."["..pos..'] =\> "'..val..'"') else print(indent.."["..pos.."] =\> "..tostring(val)) end end else print(indent..tostring(t)) end end end if (type(t)=="table") then print(tostring(t).." {") sub\_print\_r(t," ") print("}") else sub\_print\_r(t," ") end print() end -- create a background for the app local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight ) background.anchorX = 0 background.anchorY = 0 background:setFillColor( 0, 0, 0.5 ) -- Create a text object to display ad status local statusText = display.newText( "", 0, 0, native.systemFontBold, 22 ) statusText:setFillColor( 255 ) statusText.x, statusText.y = display.contentWidth \* 0.5, 160 -- Set up ad listener. local function adListener( event ) -- event table includes: -- event.provider -- event.isError (e.g. true/false ) print\_r(event) local msg = event.response -- just a quick debug message to check what response we got from the library print("Message received from the ads library: ", msg) if event.isError then statusText:setFillColor( 1, 0, 0 ) statusText.text = "Error Loading Ad" statusText.x = display.contentWidth \* 0.5 showAd( "banner", appID ) else statusText:setFillColor( 0, 1, 0 ) statusText.text = "Successfully Loaded Ad" statusText.x = display.contentWidth \* 0.5 end end -- Initialize the 'ads' library with the provider you wish to use. local appID = bannerAppID if not bannerAppID then appID = interstitalAppID end ads.init( provider, appID, adListener ) local function showBannerAd() ads.show("banner", { appId = bannerAppID, testMode = true }) end local function preloadInterstitialAd() ads.load("interstital", { appId = intersititalAppID, testMode = true }) end local function showInterstitialAd() ads.show() end local function showInterstitial() print("checking to see if the interstital is ready") if ads.isLoaded("interstitial") then print("showing the interstitial") ads.show("interstitial", { appId = interstitialAppID, testMode = true }) timer.cancel(showAdTimer) showAdTimer = nil end end local function changeAds() print("changing ad types") ads.hide() -- -- use the ads.show() to test just showing an interstitial -- use the ads.load() to test using ad.load() -- --ads.show("interstitial", { appId = interstitialAppID, testMode = true } ) ads.load("interstitial", { appId = interstitialAppID, testMode = true } ) showAdTimer = timer.performWithDelay(100, showInterstitial,0) end -------------------------------------------------------------------------------- -- UI -------------------------------------------------------------------------------- -- initial variables local sysModel = system.getInfo("model") local sysEnv = system.getInfo("environment") -- Shows a specific type of ad showAd = function( adType, vendorAppID ) local adX, adY = 0, display.contentHeight + 10000 statusText.text = "Showing " .. adType ads.show( adType, { x=adX, y=adY, appId=vendorAppID, testMode = true } ) end -- if on simulator, let user know they must build for device if sysEnv == "simulator" then local font, size = native.systemFontBold, 22 local warningText = display.newRetinaText( "Please build for device or Xcode simulator to test this sample.", 0, 0, 290, 300, font, size ) warningText:setTextColor( 1 ) warningText.x, warningText.y = display.contentWidth \* 0.5, display.contentHeight \* 0.5 else -- start with banner ad showAd( "banner", appID ) timer.performWithDelay(15000, changeAds, 1) end local function onSystemEvent( event ) print( "System event name and type: " .. event.name, event.type ) if event.type == "applicationResume" and isInterstitialShowing then print("The ad closed") showBannerAd() end end Runtime:addEventListener( "system", onSystemEvent )
Just built and tested on my iPhone 6 yesterday morning.