Ubacoda.com is an independant developer of mobile applications. Welcome to the ubacoda.com blog!

An exciting day... (Objective-C: not for the faint of heart)

Wed Mar 24 02:02:20 2010

Today is an exciting day. I received an email from Adobe saying that the new Flash Builder 4 is just on the horizon and I have received word that Flash CS5 will (fingers crossed) be available around April 22 (don't hold me to that).

Those two things alone have brightened my day, but wait... there's more. I also received an email regarding the Corona SDK for iPhone development. It had already been announced on the Corona website that Corona had planned to add support for the iPad, and not only was the iPad beta confirmed yesterday, but also Corona announced that they would also being entering the beta phase for Android support.

Now this is great for those who cannot take the excruciating pain of learning Objectve-C. "It's not that bad", I hear some say, and I must admit that after mastering my first 'Hello World', I quite agree. However, Corona feels very similar to ActionScript and in comparison to the Adobe iPhone packager that will hopefully be available next month, the use of Lua makes it much lighter.

As a programmer I am very much intreaged by all the implications... but perhaps all the 'ActionScripters' out there shouldn't get too excited yet - what with the colossal amount of programmers out there who have already made their app and are just readying their eager trigger finger for the CS5 iphone build button - it may just be too much to compete with unless your idea is truly original.

Using AMFPHP Is Not As Difficult As It Seems

Sat Mar 6 02:43:56 2010

AMFPHP is great if you want to connect to a database, mainly because the data you send is converted to binary making it's probably the fastest way to get your data from A to B. Below is a simplified explanation of how to get connected and I have included a simple class to conveniently call a connection anytime you want to send or receive data.

First of all, you will have to go to the AMFPHP Webpage and download the necessary files. (There should be a download link on the top left of the webpage). Once downloaded and unzipped, I generally change the name of the folder to 'amfphp' (if it's not already) as the version number that sometimes gets added to the name can be a little hard to remember, especially if you are working with multiple servers. However, if you think you can remember it, then you don't have to change anything. It's just something I do. You will, however, have to put this folder on your server. For this example, I will be assuming you have placed it on the root of your server, so in other words "http://yourdomain.com/amfphp".

Next up, click here to download my AMFConnect files. Included in this folder is a file called TestService.php. You will need to upload this file to the 'services' folder inside the amfphp folder you uploaded earlier. If you then go to "http://yourdomain.com/amfphp/browser/index.html you will confronted with the AMFPHP service browser where you can see all your goodies in one place. Find the TestService in the list on the left and click it. You will see a description of the testFunction that the class TestService contains. Below is a 'Call' button. Clicking will call the function testFunction and you should see "Function is working" displayed in the output window at the bottom. Any services you write in the future can be tested in this window.

Writing a AMFPHP Service

The following class is an example of a simple service that can be used with AMFPHP. You can use it as a template for other services you write simply changing the word MyService to the name of the file, and changing the function myFunction to any name you want. You can have as many functions in a service as you like, all of which will be viewable in the AMFPHP service browser talked about earlier.

<?php
class MyService {	
   /**
   @desc Write your description here
   */
   function myFunction(){
      return "Hello World";
   }
}?>

Connecting ActionScript to a Service

In ActionScript, all we really need to do to connect to AMFPHP is the following few lines of code.

var netConnection:NetConnection = new NetConnection();
netConnection.connect("http://localhost/amfphp/gateway.php");
netConnection.call("MyService.myFunction",new Responder(onRead, onFault), sendData);

On line 1 we create a NetConnection, which will allow us to connect with the world outside of the swf file that will be created upon compilation. We then pass the url of the place we want to connect to using the connect(url) method of NetConnection shown on line 2. Finally, on line 3 we use the call() method to initialize the connection passing in a few parameters.

Before I continue to explain what parameters go where, it's important to know that NetConnection can be used for a number of purposes and the following explanation is specific to connecting to AMFPHP.

Continuing on, to connect using the call() you have to pass in the name of the class and method you want to call in the form of a string. As show in the example, we can call "myService.myFunction". So if we want to connect to the TestService we uploaded and call it's testFunction, we would pass "TestService.testFunction". Next we have to pass in a Responder. As the name suggests, this will respond to our call and tell us whether is was successful or not. You can create a responder in a separate variable, but I have chosen to pass it directly into the call method for simplicity's sake. The responder will need to receive two functions. The first function it receives will be called on a successful connection to AMFPHP, and vice versus, the second will be called on a failed connection. Most people call these function onRead and onFault. In the example, theses functions have not been created, but I'm sure that if you are reading this, you will know how to do that. Alternatively, you could pass in two inline functions like this:

new Responder(function():void{/*do this on success*/}, function():void{/*do this on fault*/})

Finally, the last parameter that we can pass in is data to be sent. I generally like to pass an Object or an Array because you can send multiple sets of data in one call and easily retrieve them in the PHP files. That being say, you could simple send as little as a String or a Number. I suppose that depends on what you want to do.

Connecting the Easy Way Using My AMFConnect Class

I have to say that this is a toned version of a similar class that I created for personal use for all my connections. Admittedly, I don't feel comfortable sharing all that hard work and plus I think it helps your own understanding to be forced to create some of this stuff for yourself. But I would like to give you a big push in the right direction, so I have included a convenience class to get you more than started.

Inside the AMFConnect folder you downloaded earlier is a class called AMFConnect to do all the connection work for you. Simply change the url inside the connect call to the location of your amfphp folder on your server. The class requires two arguments. The first is the class and function you want to call in the form of a string. The second is data to be sent. To use it, first import AMFConnect and then call the following method.

var sendObj:Object = { data : "Send Data" };
AMFConnect.connect("TestService.testFunction", sendObj); 
// traces Send Data:Received Successfully
If you don't pass sendObj, the NetConnection will not send any data
MFConnect.connect("TestService.testFunction");
// traces Function is working

So there you have it. It's pretty easy to send a receive data using AMFPHP. If you are having trouble, you may need to look around for information on crossdomain.xml, which I won't go into here but should solve the majority of your problems. Anyway, for those just starting out in this area, I hope this has helped.

SharedObject - Flash Cookies

Tue Feb 16 06:04:06 2010

For anyone making applications using ActionScript 3.0, SharedObject is an absolute must! It allows data to be stored inside the flash player itself, meaning applications you create do not have to reply on browser cookies, and thus there is no worry of browser cookies being deleted or the hassle of writing a program in something like PHP to store your data on a database for example... although that may be a little more efficient especially if you are writing something that stores long-term data.

To use SharedObject, all you have to do is create a variable and datatype it to SharedObject. Don't forget to import flash.net.SharedObject if you haven't already. Take at the variable below.

var myData:SharedObject = SharedObject.getLocal("data"); 
By setting the SharedObject it will automatically be created by the Flash Player. There are no further steps involved in creating this data so it's quick and easy.

The SharedObject will accept all kinds of data including numbers, strings, arrays, etc. When data is not already set inside your SharedObject, it will return undefined. For example, I have yet to set any data in the variable 'myData' that I created earlier and so if I tried to trace any data I would get undefined.

trace("My data : "+myData.data.mySetData); //traces My data : undefined 
If I were now to actually set data and then trace it, SharedObject would return that data back to me.

myData.data.mySetData = "I set this."; 
trace("My data : "+myData.data.mySetData); //traces My data : I set this

Additionally, you can clear any data set by calling the clear() method on your variable.

myData.clear();
trace("My data : "+myData.data.mySetData); //traces My data : undefined

Naturally, that is not all there is to know about SharedObject, but it should be enough to get those who are not yet savvy about this on their feet. As a side note, this has been around for quite sometime and those game creators who haven't yet checked this out should definitely do so.

Use the Flex Framework with the Least MXML Possible

Mon Feb 15 00:54:09 2010

Using the Flex Framework is great tool to have if you know what you're doing. Sadly, not everyone has the time to put in the extra man hours to learn it, although I would thoroughly recommend trying to do so if you had the chance, as it can speed up your workflow immensely and there are so many pre-made components out there that you are really spoiled for choice.

In any regard, this post is mainly for those people who want to know how they can import components such as the ComboBox, Button, and the infamous DataGrid, and all with the minimum amount of MXML possible. However, I would just like to say before I go any further that it is probably a lot faster and easier in most cases just to learn that little bit of MXML than by doing it all in ActionScript alone... and believe me, being a big ActionScript fan, I was one of the ones who just didn't want to let go either. Anyhow, with all that said, let's get coding.

To my current knowledge, there are a few ways of getting this to work. In this post I will run through what I think is the best way of doing it. First off, you will need to create an MXML file. All files after this one can be ActionScript files, but the main project file must be a MXML file. Inside that file, you will need to place the following code.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
xmlns:classLocation="*">
	
	<classLocation:myClassName/>

</mx:Application>
I have set the layout to absolute since this will enable you to put your objects anywhere on the screen. Note that there are two more properties that you can play with; vertical and horizontal, which I won't go into now, but Flash coders for example will probably feel most at home with absolute layout.

The important thing to remember for this file is that when importing you own classes, you have to set the location so that flex can find it. I have used xmlns:classLocation="*", the * [hash] of which simply denotes 'root level', or the place where the MXML file exists. Alot of flex coders put their custom classes or components inside a folder called components and so they would use xmlns:classLocation="components.*". Finally on line 5 we call out custom class/component using classLocation:myClassName/>. The actual class name is determined in place of 'myClassName'.

Next create an ActionScript file as you would normally. The code below can be used for quick reference.

package{	
   import mx.core.*;
   import mx.controls.*;	

   public class Main {

      public function Main(){
		
         var app:Object = FlexGlobals.topLevelApplication;
         var l:Label = new Label();
         app.addChild(l);
         l.text = "Minimum MXML";

      }
   }
}
The code above simply places a label on the stage and sets the text to "Minimum MXML". The important thing to remember here is that in most cases you will need to set a reference to the FlexGlobals.toplevelapplication in order to add things to the display list, which is something I discovered recently after the move from Flex 3 to the Flex 4 SDK. On line 9 of the code above, I have created a variable called app, datatyped it to an Object and set it to FlexGlobals.toplevelapplication. Now each time I add something to the stage, I use app as a reference as seen on line 11.

And that's it. Compile the MXML file and you should see the words "Minimum MXML". If you aren't sure on how to compile MXML and ActionScript, take a look at one of my previous posts Compile AS files with Flex SDK

If you would like to download the example files for this post, you can get them from the link below.

Download Minimum MXML files

**Remember though, I do recommend learning a little MXML if that's why you are looking for this information. Don't fight it! You know that you want it really.

Passing Serialized Arrays Between PHP Pages

Fri Feb 12 05:58:43 2010

Passing arrays between pages can be a useful thing to know, particularly when you feel data is a little too long to stick inside a url and use $_GET[]. That is not to say that this is the best way of doing it. I am quite sure that there are many coders out there just shouting about a better method, or about the does and don'ts of PHP, but this, as I say, is good to know. So here goes...

First off, we need an array to pass. I will be using the one below as an example.

$arrayToSerialize = array("key1"=>"value1","key2"=>"value2","key3"=>"value3");
As you can see, it is an associative array with keys and values. To serialized the array simply call the serialized() method and pass in the array name, as in the following example. Note that I have used the variable $serializedArray to store the serialized data.

$serializedArray = serialize($arrayToSerialize);
Now we create a form to send the serialized data on a submit button click as follows.

<form action="unserialize.php" method="post">

      <input type="hidden" id="serializedArray" name="serializedArray" 
         value='<?php echo $serializedArray;?>'/>

      <input type="submit" value="Continue;">
</form>
The action of the form above points to a file we will create next called unserialize.php. The method is set to post as we will be sending the data. Inside the form, there is a hidden input which will store and pass the data on to the unserialize.php file. The name of the hidden input is set to 'serializedArray' which we will need to remember for later. Lastly, inside the form also resides the submit button that will trigger the form and send the data.



Next we need to create a file called unserialize.php as we mentioned earlier. Inside that file we write the following.

$serializedArray = $_REQUEST["serializedArray"]; 
$unserializedArray = unserialize(stripslashes($serializedArray));  
The code above captures the serialized data in a variable again called $serializedArray. Notice that the name of the hidden input inside serialize.php is placed inside a server $_REQUEST[] to retrived the data. In the line after that the data is unserialized with the unserialize() method call, passing in the $serializedArray variable. The unserialized data is them stored in another variable called $unserializedArray. The $unserializedArrayvariable can now be used as any other array. If you use print_r($unserializedArray), you will notice the results shown below are the same as the original array passed from serialized.php.



Results of print_r($unserializedArray) :

Array ( [key1] => value1 [key2] => value2 [key3] => value3 )

I have put together a quick example which you can download from the link below.

Download Serialize Array Example

Most recent posts

  1. Flashcards Maker - the app the helps you learn in a flash
  2. Convert Font Suitcase to TTF in 3 Easy Steps
  3. Rain or Shine - Weather Forecast Available Now!
  4. Getting around Apple's silly rules for iOS 10
  5. Localising App Names for iOS and Android

Search site:

Apps by Ubacoda.com

Click the app icons to see more info


Archives

Categories