Iad.Banner.new

From GiderosMobile
Revision as of 11:58, 23 August 2018 by Hgy29 (talk | contribs)

Available since: Gideros 2012.8

Description


Creates an `iad.Banner` instance. If iAd framework is not available, this function gives an error.

The alignment and the orientation of the ad banner are defined by these 4 string constants:

<ul>
<li>`iad.Banner.TOP = "top"`</li>
<li>`iad.Banner.BOTTOM = "bottom"`</li>
<li>`iad.Banner.PORTRAIT = "portrait"`</li>
<li>`iad.Banner.LANDSCAPE = "landscape"`</li>
</ul>

And `iad.Banner` class dispatches these 4 events:

<ul>
<li>`Event.BANNER_AD_LOADED = "bannerAdLoaded"`</li>
<li>`Event.BANNER_AD_FAILED = "bannerAdFailed"`</li>
<li>`Event.BANNER_ACTION_BEGIN = "bannerActionBegin"`</li>
<li>`Event.BANNER_ACTION_FINISHED = "bannerActionFinished"`</li>
</ul>

When an ad banner is created, iAd will start requesting ads. Apple serves ads, and refreshes with new ads, on an automatic timer.
Apple also requires that the ad area be hidden when no ad is available to display. iAd module manages all this behavior for you automatically.

When an ad becomes available, it will be displayed on the screen, the event `Event.BANNER_AD_LOADED` is dispatched when one is about to be displayed,
or the event `Event.BANNER_AD_FAILED` is dispatched when the loading has failed. If the loading has failed, the fields `event.errorCode` and `event.errorDescription`
contains information about the failure reason.

When the user clicks on an ad, the event `Event.BANNER_ACTION_BEGIN` is dispatched. At this time, the user will either be taken out of your application
to view the app store (in this case `event.willLeaveApplication` field will be true), or they will be presented with a fullscreen advertisement to interact with,
and `event.willLeaveApplication` will be false. In the latter case, you may decide to pause the application until the event `Event.BANNER_ACTION_FINISHED` is dispatched.

 iad.Banner.new(alignment,orientation)

Parameters

alignment: (string) whether you want ads to show top or bottom of the screen. It can be either iad.Banner.TOP or iad.Banner.BOTTOM.
orientation: (string) orientation of the banner. It can be either iad.Banner.PORTRAIT or iad.Banner.LANDSCAPE.

Examples

Example

-- if the platform is iOS, load the iAd module<br />
if application:getDeviceInfo() == &quot;iOS&quot; then<br />
	require &quot;iad&quot;<br />
	end<br />
<br />
-- 4 event listeners for debug print<br />
local function onBannerAdLoaded()<br />
	print(&quot;banner ad loaded&quot;)<br />
end<br />
<br />
local function onBannerAdFailed(event)<br />
	print(&quot;banner ad failed&quot;, event.errorCode, event.errorDescription)<br />
end<br />
<br />
local function onBannerActionBegin(event)<br />
	print(&quot;banner action begin&quot;, event.willLeaveApplication)<br />
end<br />
<br />
local function onBannerActionFinished()<br />
	print(&quot;banner action finished&quot;)<br />
end<br />
<br />
-- if iAd module is loaded and iAd framework is available, create an ad banner and then show it<br />
local banner = nil<br />
if iad and iad.isAvailable() then<br />
	banner = iad.Banner.new(iad.Banner.TOP, iad.Banner.PORTRAIT)<br />
	banner:addEventListener(Event.BANNER_AD_LOADED, onBannerAdLoaded)<br />
	banner:addEventListener(Event.BANNER_AD_FAILED, onBannerAdFailed)<br />
	banner:addEventListener(Event.BANNER_ACTION_BEGIN, onBannerActionBegin)<br />
	banner:addEventListener(Event.BANNER_ACTION_FINISHED, onBannerActionFinished)<br />
	banner:show()  -- show it because newly created banners are not visible by default<br />
end<br />
<br />
-- show the banner (if it exists)<br />
function showBanner()<br />
	if banner ~= nil then<br />
		banner:show()<br />
	end<br />
end<br />
<br />
-- hide the banner (if it exists)<br />
function hideBanner()<br />
	if banner ~= nil then<br />
		banner:hide()<br />
	end<br />
end<br />