15 May 2012
Experience building games for iOS with Flash Builder and Adobe AIR will help you make the most of this article.
Additional required other products
Intermediate
With the iAd Extension for iOS from Milkman Games, you can rapidly integrate advertisements from the Apple iAd Network into your mobile AIR application using ActionScript 3.
The iAd Extension for iOS is a completely native iAd solution implemented to Apple specifications that enables you to:
This tutorial provides details on getting started with the iAd Extension for iOS. If you want to follow all the steps below, you'll need to purchase and download the iAd Extension for iOS.
You'll need to set up iAd for your apps inside iTunes Connect before you can use the extension. Follow these steps:
Note: If your app is already configured in iTunes Connect, select it and skip to step 13 below.
Note: Take note of what you type here, as you'll need to match it exactly in your application descriptor file.
You're now ready to start building an iAd enabled AIR application.
The next step is to add the com.milkmangames.nativeextensions.IAd.ane library to your project. (If you are not using Flash Builder 4.6 and later or Flash Professional CS6 and later you will need to add the IadAPI.swc library instead.)
These files can be found in the /extension folder of the extension package zip file.
In Flash Professional CS6:
In Flash Builder 4.6:
In FlashDevelop:
You can get the iAd Extension for iOS up and running with a few simple calls. See example/IAdExample.as for a full example that shows how to create and destroy ads, set visibility, handle errors, and more.
com.milkmangames.nativeextensions.ios.*;
com.milkmangames.nativeextensions.ios.events.*;
var iAd:IAd=IAd.create();
if(!IAd.iAd.isIAdAvailable())
{
trace(“this device doesn't have iAd support.”);
return;
}
createBannerAd(), the extension will automatically handle the lifecycle of banner ads. Specifically, it will automatically try to get a new ad after one fails to load, automatically show a new ad when one is ready, and automatically hide the ad view when there's a problem—all in accordance with Apple requirements.The following example code creates a banner ad container at the top of the screen that can show either portrait or landscape content.
// Listen for events.
IAd.iAd.addEventListener(IAdEvent.BANNER_ACTION_BEGIN,onBannerActionBegin);
IAd.iAd.addEventListener(IAdEvent.BANNER_ACTION_FINISHED,onBannerActionFinished);
IAd.iAd.addEventListener(IAdEvent.BANNER_AD_LOADED,onBannerAdLoaded);
IAd.iAd.addEventListener(IAdErrorEvent.BANNER_AD_FAILED,onBannerAdFailed);
// create a banner ad container at the top of the screen
Iad.iAd.createBannerAd(IAdBannerAlignment.TOP, IAdContentSize.PORTRAIT_OR_LANDSCAPE);
willLeaveApplication property:private function onBannerActionBegin(e:IAdEvent):void
{
trace(“the user started interacting with an ad.”);
if(e.willLeaveApplication)
{
// the app is about to exit to go somewhere else.
} else
{
// the user is going to see a full screen popup. Stop sound and pause.
this.didPauseForAd=true;
pauseApplication();
stopSounds();
}
}
private function onBannerActionFinished(e:IAdEvent):void
{
trace(“the user finished interacting with an ad.”);
if(this.didPauseForAd)
{
// we had paused for the ad, now we're back, so resume
this.didPauseForAd=false;
resumeApplication();
resumeSounds();
}
}
visibility , shows the ad when true and hides it when false . When the second parameter, withAnimation , is true the ad will slide on to and off of the screen with an animated effect.private function onBannerActionBegin(e:IAdEvent):void
{
trace(“the user started interacting with an ad.”);
if(e.willLeaveApplication)
{
// the app is about to exit to go somewhere else.
} else
{
// the user is going to see a full screen popup. Stop sound and pause.
this.didPauseForAd=true;
pauseApplication();
stopSounds();
}
}
In addition to banner ads, you may be able to show a special type of ad known as an interstitial ad. These ads only work on iPads running iOS Version 4.3 or later.
Interstitial ads take up the full screen until the user is done with them. As a result, they are typically more lucrative than banner ads. Interstitial ads are most often used between two parts of your app; for instance, an interstitial ad may be shown after one game finishes, before the user can play another game.
Follow these steps to display an interstitial ad:
showInterstitial().// listen for events!
IAd.iAd.addEventListener(IAdEvent.INTERSTITIAL_ACTION_BEGIN,onInterstitialActionBegin)
IAd.iAd.addEventListener(IAdEvent.INTERSTITIAL_ACTION_FINISHED,onInterstitialActionFinished);
IAd.iAd.addEventListener(IAdEvent.INTERSTITIAL_AD_LOADED,onInterstitialAdLoaded);
IAd.iAd.addEventListener(IAdEvent.INTERSTITIAL_AD_UNLOADED,onInterstitialAdUnloaded);
IAd.iAd.addEventListener(IAdErrorEvent.INTERSTITIAL_AD_FAILED,onInterstitialAdFailed);
// show the interstitial
IAd.iAd.showInterstitial();
IAdEvent.INTERSTITIAL_AD_LOADED event. The iPad's screen will now be covered completely by the ad, and the app should stop any sounds until the ad is done. If there's an error and the iAd object can't get an interstitial ad, it will trigger IAdErrorEvent.INTERSTITIAL_AD_FAILED. Finally, the IAdEvent.INTERSTITIAL_AD_UNLOADED event will fire when the ad is being removed from the screen; at that point the app can resume its normal operation.private function onInterstitialAdLoaded(e:IAdEvent):void
{
trace(“the interstitial loaded and will show now.”);
stopSounds();
}
private function onInterstitialAdUnloaded(e:IAdEvent):void
{
trace(“unloading the interstitial ad.”);
resumeSounds();
continueWithApp();
}
private function onInterstitialAdFailed(e:IAdErrorEvent):void
{
trace(“there was an error loading the ad.”);
resumeSounds();
continueWithApp();
}
For complete examples of all available methods for interstitial ads, see the IAdExample.as file in the /example folder.
In your application descriptor XML file you need to specify the version of the AIR SDK you are using (3.1 or later) as well as a link to the extension. For a working example, see example/app.xml.
<application xmlns="http://ns.adobe.com/air/application/3.1">
<extensions>
<extensionID>com.milkmangames.extensions.IAd</extensionID>
</extensions>
If you're using Flash Builder 4.6 or later (or /Flash Professional CS6 or later), and have added the iAd extension library as described above, then you can compile as you usually do directly from your IDE. If not and you are building your app with the extension from the command line, then you'll need to specify the directory containing the com.milkmangames.nativeextensions.IAd.ane file.
Here is an example build command line:
[PATH_TO_AIR_SDK]\bin\adt -package -target ipa-test-interpreter -storetype pkcs12 –keystore [YOUR_KEYSTORE_FILE] -storepass [YOUR_PASSWORD] -provisioning-profile [YOUR_MOBILEPROVISION_FILE] iadapp.ipa app.xml iadexample.swf -extdir [DIRECTORY_CONTAINING_ANE_FILE]
When you're building a version of your app for testing be sure you use the development version of the .mobileprovision file
You can and should use the distribution version of the .mobileprovision file when building the release version of your app to upload to the App Store.
Use the tips below to troubleshoot any problems you may be having with the iAd Extension for iOS:
IAdContentSize settings when calling createBannerAd().Now that you have the iAd Extension for iOS up and running, you may want to explore the ActionScript 3 documentation or check out the other tools from available from Milkman Games.

This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.