Mike Chambers

code = joy

Detecting whether an AIR application has run before

with 4 comments

One of the things that you might need to do when building your application is detecting whether your application has run before. This can be useful if you need to initialize settings, or perhaps prompt the user with some information.

Below is a simple example that shows how to detect whether the application has run before. Basically, it checks for the existence of a file. If it doesn’t exist, then the app hasn’t run before, if it does exist, then it means that app has run.

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication
	xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
		creationComplete="onCreationComplete()">

	<mx:Script>
		<![CDATA[
			private const FIRST_RUN_TOKEN_FILE:File =
					File.applicationStorageDirectory.resolvePath("firstrun");

			private function onCreationComplete():void
			{
				if(FIRST_RUN_TOKEN_FILE.exists)
				{
					outputField.text = "This application HAS been run before";
				}
				else
				{
					var fs:FileStream = new FileStream();
						fs.open(FIRST_RUN_TOKEN_FILE, FileMode.WRITE);
						fs.close();

						outputField.text = "This application HAS NOT been run before";
				}
			}

			private function onResetClick():void
			{
				if(FIRST_RUN_TOKEN_FILE.exists)
				{
					FIRST_RUN_TOKEN_FILE.deleteFile();
				}
			}
		]]>
	</mx:Script>
	<mx:Text top="10" left="10" right="10" id="outputField"
					textAlign="center" fontSize="20"/>
	<mx:Button label="Reset First Run Status"
			id="resetButton" click="onResetClick()"
				horizontalCenter="0" width="153" bottom="10"/>

</mx:WindowedApplication>

If you need to know how many times the app has been run, then you could just write a number to the file and then increment it every-time the app runs.

One thing to keep in mind is that this is not 100% fool proof, as the user could manually delete the file. Thus you should not use this for security, serialization or trial tracking purposes.

Written by mikechambers

November 7th, 2007 at 10:35 pm

Posted in air

4 Responses to 'Detecting whether an AIR application has run before'

Subscribe to comments with RSS or TrackBack to 'Detecting whether an AIR application has run before'.

  1. Simple enough…Is it possible to save data to a Local SharedObject in AIR? Either that, or some form of “OS-standard” preference file? Coming from (minimal) Cocoa programming experience, it’s pretty easy to save preference data to a .plist file. It’s almost as easy as using an LSO… Just wondering if a mechanism like that exists for AIR, which might be a little more fool proof than just writing a single file to an almost arbitrary location, but instead in the user’s preference directory.

    Just kinda thinking out loud.

    Dru Kepple

    8 Nov 07 at 8:52 am

  2. Would it not be wiser, while a bit more complex, to use either
    1. The local SQLite database
    2. The encrypted local store
    to store this type of information. Then it could possibly be less likely that the user will delete it. I know that I am usually less likely to delete a .db file in a program directory than a .txt file that I can easily open and look at.

    Or perhaps it could be possible to create a “temporary” database that could persist through multiple sessions? That way the information would never even get saved out to a file

    Ross Ritchey

    9 Nov 07 at 4:46 am

  3. Yeah, you could use locate SQLite DB, sharedobjects, or the encrypted data store, although I think that the DB would be overkill (and more complex than it need to be).

    mike chambers

    mesh@adobe.com

    mikechambers

    9 Nov 07 at 5:43 am

  4. >Is it possible to save data to a Local SharedObject in AIR?

    Yes, you can use either a local shared object, or the encrypted data store.

    Either would work, and are pretty good at storing name / value pairs.

    mike chambers

    mesh@adobe.com

    mikechambers

    9 Nov 07 at 5:44 am

Leave a Reply