25
Mar

How to build your Flex app for iOS with AIR 2.6

AIR 2.6 brought a lot of great stuff, but one specific feature I just couldn’t pass by — publishing for iOS. Yes, there was this thing called Packager For Iphone, but now it is a part of ADT and is claimed to be much faster.

So, we have Flash Builder Burrito, Flex Hero SDK focused on mobile development and now AIR 2.6. You can see clear vector where Adobe is moving. There are many tutorials for Android platform, but iOS is lacking. So, let’s try to compile a small Flex application for iPad.

First of all, there’s documentation on help.adobe.com which helped me a lot. But there are walls of text and much of it is about Android which we don’t need here.

Apple developer program.

This tutorial assumes that you are registered Apple developer because to actually upload your app to iOS device you’ll need:

  1. Developer certificate
  2. App ID
  3. Provisioning profile

If you are not an Apple developer, you can still read the material because it is still helpful if you want to develop apps for other platforms, and who knows maybe one day you’ll join Apple developers community.

Well, first of all you need your developer certificate. Certificates are used to sign iOS applications. One for testing on devices (developer certificate) and another to publish to App Store (distribution certificate).

Basically, the process looks like this:

  1. You generate a signing request
  2. You send generated .csr file to Apple
  3. (Your corporate account manager approves your certificate)
  4. You download new certificate
  5. You combine your private and public parts of certificate in a .p12 file
  6. You use this .p12 file to sign your AIR application

Of course you need to do this only once.

Go to developer.apple.com, click on “Certificates” and follow the steps to upload certificate signing request (or read the “how to” guide on Apple developers site). Don’t worry if you are on Windows. AIR documentation has a whole section devoted to this process. There you will find step-by-step guide for generating certificates and converting them to .p12 format which AIR needs both for Mac and Windows. Just make sure that if you generated a certificate on Mac that you are converting it on Mac too (same for Windows).

In my app I put converted .p12 certificate to /certificates/ipadtest.p12 which is later used in ANT build file.

After that you need to add a new App ID for your test application. I chose just “ipadtest”. We will need this ID later.

And the last thing from Apple we will need is .mobileprovision file to be able to test our app on a real device. Don’t forget to add your iPad (or iPhone) to devices at Apple developer site.

How to find your device id.

Mobile Flex.

Adobe is doing great job with upcoming Flash Builder Burrito and Flex Hero SDK. If you are interested in Flash mobile future you should check out the videos and draft docs on Adobe Labs.

Also, there are several good articles on the web about mobile Flex development:

The last one is a good explanation of basic Mobile Flex concepts: MobileApplication, Views, Navigation bar, pushing and popping views. I definitely recommend it everyone who is curious about mobile Flex.

Also you will need ActionScript 3.0 Beta Reference.

Let’s get started.

Download and install Flash Builder Burrito with Flex Hero SDK and latest AIR SDK. You can develop in any IDE but it’s much more convenient to use the new Flash Builder, though you can’t export to iOS using it right now. We will use ANT script for publishing.

First of all add iPad to Device Configurations in Flash Builder preferences: Window -> Preferences -> Flash Builder -> Device Configurations -> Add with resolution 768×1024 and PPI 132.

We’ll be using Twitter Trends tutorial from Flex Hero SDK examples. Open the PDF in another window and download tutorial assets. Create Mobile Flex application as described in the tutorial. Don’t panic when you see the only option is to publish for Google Android. Select it and continue.

So now you have an empty Flex mobile app…

Application descriptor. General parameters.

First of all let’s modify app descriptor XML a bit. Open TwitterTrends-app.xml (or alike, if you called your project differently). More detailed information about setting up an application you can find at help.adobe.com. And right now I’ll guide you through important stuff.

First of all set namespace to 2.6:

<application xmlns="http://ns.adobe.com/air/application/2.6">

Set app id the same you added at Apple developers site but without random letters/numbers at the beginning. Warning! It’s a common mistake to copy the whole id from the site with these random numbers. Be careful!

<id>ru.interactivelab.ipadtest</id>

Add name and version info. Version must be 1-3 numbers separated by “.” and when you update your app it’s better to increase its version. Sometimes iTunes fails to update app with same version.

<filename>ipadtest</filename>
<name>iPad Test</name>
<versionNumber>1.0.1</versionNumber>

Set content manually to be able to use ANT. Make sure that it is your actual executable swf file.

<content>TwitterTrends.swf</content>

Seting visible = true might be necessary to test localy with ADL, but I think it does nothing when running with Device Emulator.

<visible>true</visible>

Set width and height.

<width>768</width>
<height>1024</height>

Set device specific parameters. Our app will be in portrait orientation. It won’t reorient. It will be fullscreen. It will be super fast using GPU. And it doesn’t need software keyboard.

<aspectRatio>portrait</aspectRatio>
<autoOrients>false</autoOrients>
<fullScreen>true</fullScreen>
<renderMode>gpu</renderMode>
<softKeyboardBehavior>none</softKeyboardBehavior>

Don’t forget to add mobileDevice to supported profiles.

<supportedProfiles>mobileDevice</supportedProfiles>

Application descriptor. iOS specific parameters.

Find the comment saying <!– iOS specific capabilities –> in application descriptor file. For detailed description refer to help.adobe.com but right now let’s replace <iPhone> block with

<iPhone>
	<InfoAdditions>
        <![CDATA[
            <key>UIDeviceFamily</key>
            <array>
                <string>2</string>
            </array>
            <key>UIRequiresPersistentWiFi</key>
            <string>NO</string>
	    <key>UIApplicationExitsOnSuspend</key>
	    <string>YES</string>
        ]]>
    </InfoAdditions>
</iPhone>

UIDeviceFamily key contains supported devices. 1 is iPhone/iPod, 2 is iPad. We are interested in iPad only. We don’t need persistent WIFI and want to close our app when Home button pressed (though it didn’t work for me).

Next we will need to add application icons and splash screens. Read this article to find out how to do it and what images you need. If you have none your app can still run but with empty white icon.

Following the tutorial.

Continue on the tutorial. Everything should be pretty easy. In design mode choose iPad as your device to see how views will be aligned on it. When asked to test run your application you will need to create a Debug Configuration for it.

I stopped when I had 2 views with data binding and item renderers. I decided to add back button so the app would look like this.

I added back button image. Get it here and put into src/assets folder. Here’s my final code for TwitterTrends.mxml.

<?xml version="1.0" encoding="utf-8"?>
<s:MobileApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
					 xmlns:s="library://ns.adobe.com/flex/spark" firstView="views.twitterHome">

	<fx:Script>
		<![CDATA[
			protected function backBtn_clickHandler(event:MouseEvent):void
			{
				if ( navigator.length > 1 ) navigator.popView();
			}
		]]>
	</fx:Script>

	<s:actionContent>
		<s:Button id="refreshBtn" icon="@Embed(source='assets/refresh48x48.png')"
				  click="Object(navigator.activeView).refresh()" />
	</s:actionContent>
	<s:navigationContent>
		<s:Button id="backBtn" icon="@Embed(source='assets/back48x48.png')"
				  click="backBtn_clickHandler(event)" />
	</s:navigationContent>

</s:MobileApplication>

Building for iPad.

Here’s what you need to build your app for iOS:

  1. Compiled SWF
  2. Application descriptor
  3. .p12 certificate
  4. .mobileprovision
  5. Icons (optional)
  6. Splash screens (optional)

Let’s get compiled SWF first. We’ll be using ANT script to compile and publish our application so I assume that you have some knowledge of ANT scripts.

First of all I have 2 .properties files. You must change local.properties and specify paths to Flex, mxmlc and adt.

FLEX_HOME=e:/work/4.5.0hero
MXMLC=${FLEX_HOME}/bin/mxmlc.exe
ADT=${FLEX_HOME}/bin/adt.bat

build.properties just lists several relative paths.

app.rootdir=.
app.descriptor=${app.rootdir}/src/TwitterTrends-app.xml
app.rootfile=TwitterTrends.swf
app.sourcedir=${app.rootdir}/src
app.debugdir=${app.rootdir}/bin-debug
app.builddir=${app.rootdir}/bin-release
app.releasedir=${app.rootdir}/release
app.source=${app.sourcedir}/TwitterTrends.mxml

build.storetype=pkcs12
build.keystore=${app.rootdir}/certificates/ipadtest.p12
build.storepass=mysecretpassword
build.mobileprofile=${app.rootdir}/certificates/ipadtest.mobileprovision
build.name=ipadtest.ipa

Here’s an archive with local.properties, build.properties and build.xml — build.zip.

Build.xml has several build targets: clean, copy (copies assets from /src to /bin-release), compile and publish-debug. Take a look at compile target. Notice that I included a number of swc’s in weird locations.

-library-path+=’C:\Program Files (x86)\Adobe\Burrito\Adobe Flash Builder Burrito\eclipse\plugins\com.adobe.flexbuilder.dcrad_4.5.0.287807\fiberSwcs\4.0\libs\fiber.swc’

These are used by Flash Builder Burrito to generate classes for data binding. Also make sure you are using airmobile config (+configname=airmobile) or compiler won’t be able to find mobile Flex components.

Next important target is publish-debug. This command performs actual packaging (and does it very slow to be honest). You can find more information about packaging to iOS at help.adobe.com.

Here’re parameters I’m using in build.xml:

-package
-target ipa-debug
-storetype ${build.storetype}
-keystore ${build.keystore}
-storepass ${build.storepass}
-provisioning-profile ${build.mobileprofile}
${app.releasedir}/${build.name}
${app.descriptor}
-C ${app.builddir} ${app.rootfile} assets photos sampledata

You can build for several targets: ipa-test, ipa-debug, ipa-ad-hoc, ipa-app-store. With ipa-debug you can debug your app remotely on the device. Read more about remote debugging.

Testing on iPad.

After you’ve compiled your app into .ipa file and before you will be able to actually test it on your iPad, you need to drag .mobileprovision file to iTunes to indicate that it is authorized to install this specific app to the iPad with specific UID. You might need to drag .p12 file used too but I’m not sure if I did that or not.

Sync with iPad and check the app on device.

If you have problems syncing check that you have correct app id in app descriptor xml, that you provided correct iPad UID and didn’t mess anything up while converting certificates.

Conclusion.

While Adobe is doing great job trying to make AIR run everywhere, iPad performance is very poor so far. I’m getting huge interface lags even with this simple app. We even made a video which I posted earlier where you can see it yourself.

I hope that one day I’ll be able to create competitive iPad apps with AIR, but for now I’ll agree with Steve Jobs and will keep Flash away from my device.

Tags: , , , , , , , , , , , , , , , ,

  • zorro

    you need a Mac also!!!!
    no future for flex/air…
    i switch to html5…
    ciao

    • Nate

      Uhhh… No you don’t. I just created my .ipa with my windows dev box.

    • martinez

      your obviously a noob who is clueless about the industry

  • AS4More

    Would ipa-app-store not have a performance increase over wifi remote debugging with ipa-debug !
    Please try and report back?

  • http://invisionsta.com respectTheCode

    To enable remote debugging I added -debug=true to the compile arguments and -connect to the publish-debug arguments.

    You also need to create a new debug profile from the Web Application group. Give it the same project name and set the url to about:blank.

    Its pretty slow but it works.

  • http://www.adobe.com Matthew Horn

    Great article! I tried this out with the command line tools. The only thing I had to do differently was remove the “-C” option from adt. It initially complained that I had too few arguments (my app used fewer files).

  • http://www.riagora.com Michael Chaize

    Hi. Great article !
    @zorro: I don’t see why you need a MAC. Actually, you can develop and build your IPA on a Windows PC.

    Regarding the actual poor performance, it will be fixed this year. We’ll communicate the date soon. Here’s a preview of the future performance of Flex on iOS: http://www.riagora.com/2011/03/preview-of-flex-on-ios/

    • zorro

      @Michael Chaize: …and where should i upload the ipa? is on mac only !!! pc not allowed :(
      Adobe knows that and say nothing about mac uploader – this is very bad i will never buy an adobe product…
      ciao

      • enigma

        Zorro, You are an idiot. If you dont want to buy an adobe product then f off. A lot of people are happy with adobe products. Show ur flash/air hate somewhere else.

  • Bogguard

    Hi, i’m trying to run a sample app manually and now tried your example with the ant script, but always got the same error back.

    [exec] Compilation failed…
    [exec] Result: 12

    Any idea what might be the cause?

    • http://va.lent.in Valentin

      did you edit local path variables? do you have latest SDK and AIR?

  • Bogguard

    Valentin, i edited the local path variables. I have SDK 2.6 of air and downloaded the latest burrito flash builder.
    I dropped the air SDK into the 4.5.0 sdk of flex.
    If i change some filenames in the ADT command it gives usefull errormessages, but when everything seems to be correct the ADT command takes about 5 seconds and then gives the verbose error ‘compilation failed…’.

    • aaronyeo

      If you are use the flash builder burrito. you need to add -target-player=11 to Additional compiler arguments.

      • Bogguard

        Was because of jdk7.
        Going back to jdk6 solved it

        • Sandeep

          What do you mean by JDK7 and 6 solved it. Not sure how i can change it

  • http://www.dreamsandcreations.be Bart

    I got “UIApplicationExitsOnSuspend” working by using:
    UIApplicationExitsOnSuspend

    • http://www.dreamsandcreations.be Bart

      Sorry the brackets got removed

      <key>UIApplicationExitsOnSuspend </key>
      <true />

  • Adrian

    Thanks for this tutorial. I’ve had a number of challenges getting it to work, including properly installing AIR 2.6 on top of Flex 4.5. But I think I have all that working. My current problem is that I get a “Unknown package target ipa-debug” because it is looking for apk targets, not ipa targets. How do I tell ADT to target the iOS platform instead?

    Thanks again.

    • http://va.lent.in Valentin

      are you running ADT from command line? are you sure you have 2.6 namespace in app descriptor? are you sure you installed 2.6?

  • Adrian

    Initially I was getting an error because the 2.6 in the app descriptor was not matching the version of ADT. Once I figured out how to install AIR 2.6 on top of Flex 4.5, I stopped getting an error with the version of AIR. I have also done a ADT -version to confirm that it is 2.6. I am running ADT from the ANT script you provided, with changes to point to my folders.

  • Adrian

    I found the problem. Had not updated my local.properties path for Flex SDK which was pointing to my pre-AIR 2.6 version. Build success!

    • http://va.lent.in Valentin

      great

  • lb

    Hi, there’s obviously a lot of confusion of how to install Air 2.6 sdk over flex 4.5. I believe I’m having the same problem. Not sure what the deal is. I’m getting an invalid application descriptor for unknown 2.6 namespace when I publish. Are you able to clarify the steps involved for that part.

    Also, why are you building through ant? Is it just your preference or is it necessary. That’s not explained and it might be helpful to understand why.

    Thanks

    • http://va.lent.in Valentin

      it’s just my preference
      other than using long string in command line

      copying air 2.6 over 4.5 sdk should work

  • Adrian

    It may very well work to copy air over 4.5 sdk, but what I did was follow directions for an earlier upgrade using ditto:

    http://mchristoff.com/2009/12/installing-the-adobe-air-2-beta-sdk-on-os-x/

    That worked well for me.

  • Bernd

    Hi, thank you for your tutorial. I followed your description to build and deploy an iOS app. It’s working fine for simple apps. But when I try to build an app with an additional SWC library, I ran into an exception when I’m using the ipa-debug target.
    If I use the ipa-test target it builds the app.
    The exception occurs not in the ant output, instead a window is popping up:
    Title: Microsoft Visual C++ Runtime Library
    Content: Runtime Error! Program: C:\WINDOWS\system32\jaba.exe

    Do you have any idea what causes the problem?

    • http://va.lent.in Valentin

      no idea
      what the hell is jaba.exe ?

      • Bernd

        Sorry, I mean java.exe :-)

  • Bernd

    Hi, I got now an answer from Adobe. Its a bug of AIR 2.6. It will be fixed in the next AIR version.

  • JDH

    Does Flex on iOS support text selection in textarea and context menus for textareas and webViews?

  • Bastian

    just tried your example with the new AIR 2.7 sdk… works really nice and much smoother than with the old 2.6

    I didn’t change anything except the namespace in the app-descriptor and the local property file targeting the new sdk

  • http://www.kral--oyun.com/ kral oyun

    A few weeks ago, one of our Flex QA engineers, Shashwati Keith, built a mobile app in Flex and deployed it to the Android Marketplace. The app is Muni Tracker. Muni Tracker lets you track San Francisco Muni vehicle arrival time predictions on a map updating live every 10 seconds. Since Shashwati built this app, internal builds of Flash Builder can now target iOS devices (iPad, iPhone, etc.). Because Shashwati is super busy getting the upcoming release of Flex 4.5 and Flash Builder ready for the world, I volunteered to take her Flex project and attempt to get the app running on my iOS devices and video the entire conversion. It actually turned out to be almost too easy! Basically, I set up the Apple cert and provisioning profile, made a couple of minor tweaks to the app config XML, and got it to work without touching a single line of Flex code. The 12 minute video is below. The total conversion time from importing the Flex project and demonstrating the app running on my iPad 2 was less than 10 minutes! Without my commentary, I can do it in 6 minutes (1 minutes for import/modifications, 5 minutes for iOS packaging and deploy).

  • Tanweer

    How to create .mobileprovision file? Can i create my own?

    • http://va.lent.in Valentin

      you have to be in Apple dev program which is paid service.

  • andrew

    Is there any way I can test my app without being registered as an Apple developer(without Developer certificate things..)?

  • Robert

    I am trying to convert a flash file into an application by publishing it to AIR 2.6 in Flash Professional CS 5.5. Everything works fine, including the video playback. Howevever, I have a button that accesses a Unity 3d game file (.exe) which doesn’t work. It works when I publish to a windows projector file. I used the fscommand to access the Unity file. Does anyone have any suggestions??

    • http://va.lent.in Valentin

      Use NativeProcess.