开发者

AS3 Saving Local File under iOS

I am opening and testing data using the FileStream class which is supposed to avoid file security issues. It works fine until I try to save to a local file. When I test under adl I get a security error which I though was ok (at least it was attempting to save) until the app was packaged and tested on a device, but the file fails to save. Any ideas?

public class FilesApp extends MovieClip {

    var file:File;
    var xmldata:XML;

    public function FilesApp() {
        // constructor code

        LoadButton.addEventListener(MouseEvent.MOUSE_DOWN,xml_load);
        ParseButton.addEventListener(MouseEvent.MOUSE_DOWN,xml_parse);
开发者_如何学C        SaveButton.addEventListener(MouseEvent.MOUSE_DOWN,xml_save);
    }
    public function xml_load (e:MouseEvent):void
    {
        file = File.applicationDirectory;
        file = file.resolvePath("./Data/data.xml");

        var fileStream:FileStream = new FileStream();
        fileStream.open(file, FileMode.READ);
        var str:String = fileStream.readMultiByte(file.size, File.systemCharset);
        fileStream.close();
        DataText.text = str;

        xmldata = XML(str);
        trace('xml file loaded');
    }
    public function xml_save (e:MouseEvent):void
    {
        var writeStream:FileStream = new FileStream();
        writeStream.open(file, FileMode.WRITE);
        writeStream.writeUTF("<xml><name>Changed</name><score>5000</score></xml>");
        writeStream.close();
        trace('xml file saved');
    }
    public function xml_parse (e:MouseEvent):void
    {
        trace('xml file saved');
        DataText.text = "Your name is:"+xmldata.name+" Your best score is:"+xmldata.score;
    }
}


You cannot save data in File.applicationDirectory, it is meant to be immutable. Store it in File.applicationStorageDirectory instead. There is also File.documentsDirectory - current user documents directory, if you are sure you need to litter it with your files :)


For me, switching to readUTF() worked.

Also, the two lines initializing "file" should be in the constructor, else you cannot call "xml_save" (you'll have to do "xml_load" first).

Thanks


did you try Filereference.save()?


It fails because you are using writeUTF to do the write and readMultiByte to do the read.

If you switch to readUTF it will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜