Hi, i’m wondering how should i read and store files on ios.
Flow that i want to achieve is:
- Download file from web.
- Save it on local storage.
- Next time app starts check if local file exist, if it’s there load it from local storage instead from web.
For number 1 there is no problems, i’m using URLLoader and that is fine but what should i use to deal with other steps ?
I’ve come up with this solution, maybe someone finds this helpful
var l: URLLoader = new URLLoader();
l.dataFormat = URLLoaderDataFormat.BINARY;
l.addEventListener(Event.COMPLETE, onLoadComplete);
l.load(new URLRequest("file url"));
function onLoadComplete(e:Event)
{
var path:String = SystemPath.applicationStorageDirectory + "/image.png";
File.saveBytes(path, l.data);
if(FileSystem.exists(path))
{
var loader:Loader = new Loader();
loader.load(new URLRequest(path));
addChild(loader);
}
}
Is there any more elegant way to do the same ?
I would try doing this, warning don’t know if it compiles or works
var l: Loader = new Loader();
l.addEventListener(Event.COMPLETE, onLoadComplete);
var path:String = SystemPath.applicationStorageDirectory + "/image.png";
if(FileSystem.exists(path))
{
l.load(new URLRequest(path));
}else{
l.load(new URLRequest("file url"));
}
function onLoadComplete(e:Event)
{
var loader:Loader= cast e.target;
loader.removeEventListener(Event.COMPLETE,onLoadComplete);//important to remove listeners
add(loader);
var path:String = SystemPath.applicationStorageDirectory + "/image.png";//you should try to get the name of the file from the URL, to make this function file independent.
if(!FileSystem.exists(path))
{
File.saveBytes(path, loader.contentLoaderInfo.bytes);
}
}
Hi, thanks for replay and great example. I’ve started learning HAXE + openFL few days ago and really appreciate examples like this one and hope others will too