Creating a custom application updater
In above image you can see the custom application updater interface I have created for my NoteList application. You would see this updater interface only if updated application is available for download. Clicking the ‘Download and Upload’ button downloads updated .air file and updates current application.
Below you can see the source code for updating an AIR app. The following code shows you how to:
1. Load the update descriptor file.
// load the update-descriptor var loader:URLLoader = new URLLoader; loader.addEventListener(Event.COMPLETE, onLoadComplete); loader.load(new URLRequest('http://mariusht.com/files/products/notelist/update.xml'));
2. Check and see if a user has the latest version of an AIR app installed.
private function onLoadComplete(event:Event):void { // ignore xml declaration var str:String = URLLoader(event.target).data as String; var index:int = str.indexOf('<update'); str = str.substr(index, str.length); // convert String to XML var remoteAppXML:XML = XML(str); var ns:Namespace = remoteAppXML.namespace(); // get new version number var version:String = remoteAppXML.ns::version; // get current version number var appXML:XML = NativeApplication.nativeApplication.applicationDescriptor; var ns:Namespace = appXML.namespace(); currentVersion = appXML.ns::version; if(currentVersion != version) { // download AIR file } }
3. Download and save the AIR file. Don’t forget to call Updater.update() function.
public class UpdateAppCommand extends SimpleCommand implements ICommand { override public function execute(note:INotification):void { // download updated .air file urlStream = new URLStream(); fileData = new ByteArray(); urlStream.addEventListener(Event.COMPLETE, loaded); urlStream.load( new URLRequest('http://mariusht.com/files/products/notelist/NoteList.air')); } private function loaded(event:Event):void { urlStream.readBytes(fileData, 0, urlStream.bytesAvailable); writeAirFile(); } private function writeAirFile():void { var file:File = File.applicationStorageDirectory.resolvePath('NoteList.air'); var fileStream:FileStream = new FileStream; fileStream.open(file, FileMode.WRITE); fileStream.writeBytes(fileData, 0, fileData.length); fileStream.close(); var versionProxy:VersionProxy = facade.retrieveProxy(VersionProxy.NAME) as VersionProxy; var updater:Updater = new Updater; updater.update(file, versionProxy.version); } private var urlStream:URLStream; private var fileData:ByteArray;
Related Links:
Updating AIR applications (Adobe AIR 1.5 LiveDocs)
Leave a comment