Nick Kuh Open Source Component Library

23.10.2008 0

From now on any useful Adobe Flex or AIR code example or component will be available to download from my new Google Code Repository.

If you checkout the svn repository straight into Flex you should see it automatically build a Flex Library for you, including the swc file that contains all the source.

Hope you find them useful!

Auto centre main AIR Application window with code

17.10.2008 1

Here’s a tip for how to centre (center for you yanks) your main Adobe AIR application window at runtime. It’s not as obvious as this.x = numX, this.y = numY for some reason. All this does is ends up moving you application container out of the NativeWindow masked view. Not what I was after.

You can set the initial position of your main application window in your app descriptor xml (although I can’t see the point of this as most developers are going to want it set dynamically based on the dimensions of the user’s screen).

So here’s the code that get’s called once the windowComplete handler is called:

private function centreApp():void
{
var screenBounds:Rectangle = Screen.mainScreen.visibleBounds;
var w:int = width;
var h:int = height;

var x:int = screenBounds.x + ((screenBounds.width-w)/2);
var y:int = screenBounds.y + ((screenBounds.height-h)/2);
stage.nativeWindow.x = x;
stage.nativeWindow.y = y;
visible = true;
}

In the main application I’m building I’m using the PureMVC Flex Framework and I’m not actually running this code snippet until my config xml and my styles swf has completed load into the main application so my code retains full control of when the application displays and where it displays :-)

Download the App

Download the Source

Regular Expressions in Flex and AS3

16.10.2008 0

I’ve needed to user Regular Expressions a couple of times since they were introduced in ActionScript 3 and invariably find myself trawling the Regular Expression tutorial/example websites out there which in general I find very confusing! I don’t think Regular Expressions are that difficult to grasp (the basic anyway) but none of the example sites I’ve found have clear explanations of the syntax.

I’m currently developing a Crossword Application in Flex for a client which is pretty much complete. However, one of the bugs I had to resolve today was being caused by some unforseen incompatibility issues with the solution text node coming from the client’s huge database of crosswords xml files. If a solution for example had a space, apostrophe, hypen or various other punctionation it needed to be stripped out of the node by my code to ensure that the correct number of cells were generated on my crossword app grid. So here’s an example of using a regular expression to do just that:

var notCharOrNum:RegExp = /[^A-Za-z0-9]/gi;
var myText:String = "The bee’s knees";
myText = myText.replace(notCharOrNum,""); //outputs “Thebeesknees”
trace(myText);

In summary the reg expression means match everything that isn’t either a letter or a number. The ^ bit is the special character than means except these characters. The g means global – I think that just means the match continues searching throughout the string until it reaches the end. The i means case insensitive search so I guess this would do the same thing:

var notCharOrNum:RegExp = /[^a-z0-9]/gi;
var myText:String = "The bee’s knees";
myText = myText.replace(notCharOrNum,""); //outputs “Thebeesknees”
trace(myText);

Yep – just checked and it does!

Flash Player 10 – Official Launch

15.10.2008 0

The official launch of Flash Player 10 went live a couple of hours ago.

I’ve just downloaded and and installed the update and am pleased to find that the debug version is working perfectly with Flex Builder 3’s debugger (see past post for issues last time I updated).

So go ahead and download the update!

Flex RIAs can’t call Google APIs

14.10.2008 2

I’ve been looking into ways to access the Google APIs from Flex recently for an Air Project I’m planning to build in my spare time.

I managed to get the following process working fairly easily in AIR:

Step 1: User enters their Google login details which then get passed to google’s https://www.google.com/accounts/ClientLogin API login script which returns a string of variables including the google session authorization variable.

Step 2: Using the Auth variable passed back from a successful google login you can then call the many Google data APIs available – in the example I built after a successful login the app simply retrieves the user’s google contact list.

This all seemed to be working fine in Adobe AIR.

It was when I tried to port this to a web based RIA that I started running into obstacles.

Most of the problems are down to google’s lack of / restictions in their crossdomain.xml

There’s a crossdomain file here:

http://www.google.com/crossdomain.xml

But it doesn’t include the <allow-access-from domain=”*” /> tag  :-(

Plus there’s no crossdomain.xml file on google secure domain https://www.google.com domain which needs to be called to get the authorization variable to access the various APIs.

Even when you run a Flex app locally in a browser you run into further problems if you ever get past the login stage you then need to send the ‘Authorization’ head with “auth=[the_key]” with every API call in the header of you HTTP request.

But guess what? As of flash player 9.0.115 the “Authorization” header was been blacklisted! So it just gets ripped out of the request and the call to google fails!

From reading other posts I understand that you can now use the Authroization header with more recent versions of Flash Player but…

The site you’re calling requires this to be added to the crossdomain file:

<allow-http-request-headers-from domain="*" headers="Authorization"/>

More info from Adobe here:

http://kb.adobe.com/selfservice/viewContent.do?externalId=kb403184&sliceId=2

No – that node doesn’t not exist in Google’s crossdomain file! Lol.

So, my next attempt to get round this obstacle involved downloading Abdul Qabiz’s as3httpclient library for adding headers to your http requests from Flex.

This actually gets round the header issue very well and did allow me to call Google Contacts API request url and include the required Authorization header. Locally that is…

As soon as I then tried to run the working files on my remote web server I saw that for socket calls too (this is how the as3httpclient make requests) also requires a crossdomain.xml for socket calls :-(

I give up – am just going to stick to building an AIR app where it all just works…