Wednesday, February 2, 2011

Hacking an iOS app from an existing Flex app


To make an iPhone application work from an existing Flex/AS3 app, you have to jump through a few hoops.  The first and foremost are getting a few certificates, png's and licenses out of the way.  Before going any further, you need the following:

1) Provisioning profile
2) PKCS12
3) icons
4) an app (not necessary before you start, but indeed necessary for the end result)
5) PFI (Packager For IPhone)
6) AIR SDK (2.0.2 or later)

Ok, the provisioning profile is the hardest bastard to deal with.  If you are like me, and just want to put together a proof of concept, you don't (nor shouldn't) have to pay a company $99 just to try it out.  However that's not how it works for iOS applications.  Apple wants your money any way they can get it, and even if you want to write just a simple app, they'll try and get it however they may.  After asking Lord Google the best way to get past paying money, I found this one link with a hacked provisioning profile...success!!!  Actually, I need to learn the mechanics of this profile to understand what's really in there.  But for my POC, I just grabbed one from here.

Next, I needed a PKCS12 (or more short-handedly known as a .p12) certficate.  This wasn't easy because I've never created one before.  After much searching and trying, I came across Flat Mountain's awesome posts on how to do it.  Always looking to improve my shell skills, I followed his post and finally got some certs built.  I could've used Apple's developer site, but that seemed to obtuse.  and this is for OSX.4, so it might not be relevant, but it's also a start.  On a Mac you can even use Keychain Access, but I didn't go the "easy" route until Flex Builder started complaining that my cert wasn't a "code-signing" cert...  I spent another few minutes trying (However, now that I'd gone through the process of making the cert manually, I understood a lot more about what keychain access wanted to do).  In less than 3 minutes I had created a CA and a cert…but dangit! I couldn't export those as code signing certs (at least not right off the bat, even though I had created them as code signing certs).  About 2 minutes later, i figured I had to evaluate the cert.  I changed the viewing and evaluation to Code signing and voila! I could export the cert as a .p12!  WOOT!

OK, fine, I should also post that exporting the final release will also give you the option of creating your own .p12 right there on the spot, but I felt like that is just way too easy to do, and you don't actually have a clue what's going on in the black magic voodoo of Flex Builder's sneaky background (maybe a post is required about how FB builds keys…but not right now).

Armed with my cert and profile, I just needed a quick default icon, so I used some Photoshop (which isn't necessary, but can help), then Apple's Icon Composer (part of the xCode package: /Developer/Applications/Utilities/Icon Composer).  This guy's blog post helped, but I wasn't interested in paying for CandyBar, so I skipped that step .  It's also about 4 years old, so unless you're still running just Leopard or Tiger, and CS3 or CS4, then you have to use a little imagination.

Ok, icons and certs down, now to getting my AIR app to work.  I used FB4's converter to change the project from Flex to AIR.  A few errors (read: Flex 3.5 SDK custom preloader doesn't like AIR applications because of the addedToStage event.  With a workaround in my custom preloader, I have a functioning AIR app that works on my desktop.)

Now, command line is cool, but sometimes we just need functional.  I got the steps from Kevin Cantrell's post to start with, but I didn't like figuring out all the paths, tabbing through and hopefully not messing up the spelling of anything (I'm lazy, not a hipster).  Even after making it all, putting it into a text editor so I could copy and paste for later use seemed trite.  So I used FB's export tool with my custom signed certs to at least build my app's swf (it exports as a .air, but that's just code for .zip/.tar/.air/.whatever).  I un-tarred it to have the swf, but there's also a copy in my bin folder by default.  I also want to be lazy, so I used this post from Ivan to update the ant build process so I could remain lazy.  Now I have everything in place, so let's let it run, and tada!!!

I've got a functioning Flex app converted from Flex 3.5 to iOS (with some hefty mods). and it runs!  Ok, it isn't native, but as a POC, it doesn't matter.  The point is the point.

This was for me a POC as well as an aggregation of different user's experiences.  If you have any questions, please ping me or the other guys who put this together.

No comments:

Post a Comment