Michael Kaufman
Michael Kaufman

Reputation: 803

Adobe AIR 3.4 saving/writing XML data to iOS

I'm trying save XML data to an iPad 4 with AIR 3.4 for iOS and really can't tell if this is working or not. No events are being fired apparently. Any help is greatly appreciated.

    private function saveData(e:MouseEvent):void {




        var name:String = AssetManager.SAVE_ANNOTATIONS_NAME            
        var file:File = new File()              
        file = File.applicationStorageDirectory.resolvePath(name + "xml");
        var xml:XML = _canvas.getObjectData(FormatType.DEGRAFA);            
        var fileStream:FileStream = new FileStream();
        fileStream.open(file, FileMode.WRITE);
        fileStream.writeUTF(xml.toString());


        fileStream.addEventListener(ProgressEvent.PROGRESS, onFileStream);
        fileStream.addEventListener(Event.ACTIVATE, onFileStream);
        fileStream.addEventListener(Event.OPEN, onFileStream);
        fileStream.addEventListener(Event.DEACTIVATE, onFileStream);
        fileStream.addEventListener(IOErrorEvent.IO_ERROR, onFileStream);
        fileStream.addEventListener(Event.COMPLETE, onFileStream);



    }

    protected function onFileStream(event:Event):void
    {
        trace('filestream event was ' + event)
        fileStream.close();


    }   

Upvotes: 1

Views: 2704

Answers (3)

James Harding
James Harding

Reputation: 21

You must take note of the command you are using to open the file stream. If you use filestream.openAsync then the file stream is opened asynchronously, meaning you will have to wait for the Event.OPEN event on the filestream object. If you use filestream.open, then you can call functions on the filestream object immediately without waiting for an OPEN event. For me the jump to ActionScript, and its asynchronous nature, took some time to get used to.

Upvotes: 0

Naviator Dev
Naviator Dev

Reputation: 1

You are writing BEFORE adding the listeners. Proper order should be:

    var fileStream:FileStream = new FileStream();

// all the listeners here

    fileStream.open(file, FileMode.WRITE);
    fileStream.writeUTF(xml.toString());

Upvotes: 0

Michael Kaufman
Michael Kaufman

Reputation: 803

I seemed to have fixed it with this:

    private function saveFile(event:MouseEvent):void
    {  
        var xml:XML = _canvas.getObjectData(FormatType.DEGRAFA);
        trace('xml is ' + xml.toXMLString())
        var file:File = File.documentsDirectory.resolvePath("annotations.xml");         
        var fileStream:FileStream = new FileStream();  

        fileStream.openAsync(file, FileMode.WRITE);  
        fileStream.writeUTFBytes(xml.toXMLString()); 
        fileStream.addEventListener(Event.CLOSE, fileClosed);  
        fileStream.close();  

        function fileClosed(event:Event):void { 

            trace("File Saved");  

        }        
    }  

Upvotes: 3

Related Questions