mike chambers | about

Detecting whether an AIR application has run before

Wednesday, November 7, 2007

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"?>
	xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
			private const FIRST_RUN_TOKEN_FILE:File = 
			private function onCreationComplete():void
					outputField.text = "This application HAS been run before";
					var fs:FileStream = new FileStream();
						fs.open(FIRST_RUN_TOKEN_FILE, FileMode.WRITE);
						outputField.text = "This application HAS NOT been run before";
			private function onResetClick():void
	<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"/>

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.

twitter github flickr behance