Mike Chambers

code = joy

Detecting whether an AIR application has run before

with 10 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

10 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

  5. This also doesn’t take into account the fact that the user may have had the app installed before and uninstalled.

    I am unable to find a way to get around this.

    Tony MacDonell

    9 Jul 09 at 1:20 pm

  6. @Tony, If the file is placed in the appstorage directory (may be assets folder), it gets deleted when the application is uninstalled. With this you can first test if file exists or not. If it doesn’t than it means its a fresh install or reinstall and you can safely delete the existing db file.

    Yash Mody

    6 Aug 09 at 9:09 am

  7. @Yash
    It is not deleted when the application is uninstalled. Is there a solution for this? Thanks!

    Jugger

    27 Oct 09 at 7:19 pm

  8. Is there a way to delete the applicationStorageDirectory on uninstall?

    On an app I am working on, I must display an EULA popup the first time the app is run. If a user uninstalls and reinstalls, the EULA must be displayed again.

    Any ideas?

    jr

    5 Nov 09 at 1:10 pm

  9. @Jugger,.

    I think this is a windows OS specific problem. It deletes the file on MAC OS I ll verify it on windows though

    Yash

    Yash Mody

    16 Nov 09 at 1:10 am

  10. @jr
    I’d recommend using the applicationDirectory and not the applicationStorageDirectory if you want stuff to be deleted during uninstall.

    Jeremy Daley

    7 Jul 10 at 11:03 am

Leave a Reply