Actionscript & PHP Development

Building apps using Adobe Air, Actionscript 3 and PHP

By
Warning: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=,&sensor=false): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home1/foncused/public_html/dev/wp-content/plugins/google-seo-author-snippets/microdata_form.php on line 228

Warning: file_get_contents(http://maps.googleapis.com/maps/api/geocode/json?latlng=,&sensor=false): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request in /home1/foncused/public_html/dev/wp-content/plugins/google-seo-author-snippets/microdata_form.php on line 228
Richard Mackenzie

Storing data on an IOS app with SQLite and FSO

There are many ways you can store data in you IOS or Android application, today I will run you through 2 types, Flash Shared Objects (flash cookies or FSO) and SQLite.

Flash Shared Object (FSO)

Zombie attackThis option is great for when you need to store small amounts of data such as a few variables. For example if you want to store a single highscore for a game like I needed to do for my IOS game Zombie Attack. First we place in this line “var mySharedObject:SharedObject = SharedObject.getLocal(“zombieattack”);”, this creates a file on the device that will store all your info (at the time or writing this, if you delete the app and re install it, your data will still be there as it is not stored on the app but rather in the phones documents directory)

So in the example below you can see I create a FSO called “zombieattack”. I then check to see that the username field is not blank, then I set all the variables I want to store in the FSO. Then run the "mySharedObject.flush();" command to store the data inside the FSO.

var mySharedObject:SharedObject = SharedObject.getLocal("zombieattack");

if(name_txt.text == ""){
  error_txt.text = "Please enter your name";
  }else{
  entername = name_txt.text;
  mySharedObject.data.firstName = entername
  mySharedObject.data.totalscoresave = totalpoints
  mySharedObject.flush();
  newhighscore = true;
  gotoAndStop(18);
}
									

Then when you need to recall the data its as easy as calling a variable. If you ever need to empty out the FOS you can use this command "mySharedObject.clear();". If you close and open the app again the variables you set in the FSO will still be there.

 

SQLite


SQLite is used when we want to store large amounts of data and currently is the only DB supported by IOS and Adobe Air for IOS. As with FSO’s the actuall db is not stored insite the application but instead in a documents folder on the physical device.

SQLite Flash

Once you have created the db, you need to embed a copy of the db inside the app (this can be found under settings on the properties panel). Then each time the app launches we will check to see if there is an existing version of the SQLite db on the phone.
If it cant find one on the application we will take a copy of the db we embedded inside the app and copy it across onto the device (again at the time or writing this, if you were to delete the app and re install it your origonal db would still be there with all your old data). Now next time we run the application it will check to see if the db is there, and it will find the version we copied across.

The variable "dbFile:File" is the location of the db we embedded inside the application and the variable "dbWorkedFile:File" is the location where we are going to copy a version of the db should one not exist.

var dbFile:File = File.applicationDirectory.resolvePath("Score.db");
var dbWorkedFile:File = File.documentsDirectory.resolvePath("Score.db");
 
if(!dbWorkedFile.exists){
trace("no database, start coping")
 
dbFile.copyTo(dbWorkedFile);
 
}else{
                trace("database exists")
                }    

									

With Adobe Air you are able to connect directly with SQLite db’s and run SQL queries from within the app. This will be covered in another tutorial.