Unable To Write To Documents On Xcode Simulator

Hello all! I came across a problem i can’t solve myself.

Please help :slight_smile:

 

I want to save game state to a json file. It works perfectly fine in Corona Simulator, but doesn’t work in XCode
simulator. It seems like the XCode simulator is not able to write the file to the Documents directory
in its XCode sandbox folder. Although it reads files frome there if I manually put them there in finder.

As I mentioned - in Corona Simulator it loads and writes files perfectly fine.

 

And yes - I know that for the first time there are no files in Documents folder, so for the first time
I read initial game state from the Resources folder (after checking if in Documents folder there’s no file),
and on application exit I write the new game state to the Documents folder.
From now on after second app start I read the state from there.

 

In Corona simulator it works, in XCode simulator (6.1 on mountain lion) it doesn’t.
I know that with new XCode the sanbox path has moved - but if it was the issue, then XCode
wouldn’t read the files from system.DocumentsDirectory neither, but it reads them
(if I manually put them there in finder)…

 

I lost my hope. Do you have any suggestion why this doesn’t write files in XCode only?
Or maybe it’s normal in xcode simulator but on the actual device it will be fine?
(I’m still checking the trial so cannot test it on the device).

 

Please help :slight_smile:
Cheers to all!

 

PS. I attach my code below (only for saving function)

 

local JSON = require ("json"); local function saveState() local t = {}; --here I put some data to t table local jsonString = JSON.encode(t); local base = system.DocumentsDirectory; local path = system.pathForFile("save.json", base); local file = io.open(path, "w+"); file:write(jsonString); io.close(file); end local function onSystem(e) if(e.type == "applicationExit") then saveState(); end end Runtime:addEventListener("system",onSystem);

 

 

UPDATE - the function itselfs works (I invoked it now with a button), it’s just it doesn’t work no system event “applicationExit” in XCode. In Corona it does work :expressionless:   

 

Any ideas, why on “applicationExit” doesn’t work in XCode Simulator?

below is the code for that part. Pretty straight forward.

local function onSystem(e) if e.type == "applicationExit" then saveState(); end end Runtime:addEventListener("system",onSystem);

 

 

SOLVED - I had to change the event to on “applicationSuspend” :smiley:

I guess the applicationExit is to fast for saving :slight_smile:

 

Anyway I leave this post for others

Keep in mind that on iOS apps really don’t exit unless you force kill them or they crash.  Apple wants them to suspend into the background.

UPDATE - the function itselfs works (I invoked it now with a button), it’s just it doesn’t work no system event “applicationExit” in XCode. In Corona it does work :expressionless:   

 

Any ideas, why on “applicationExit” doesn’t work in XCode Simulator?

below is the code for that part. Pretty straight forward.

local function onSystem(e) if e.type == "applicationExit" then saveState(); end end Runtime:addEventListener("system",onSystem);

 

 

SOLVED - I had to change the event to on “applicationSuspend” :smiley:

I guess the applicationExit is to fast for saving :slight_smile:

 

Anyway I leave this post for others

Keep in mind that on iOS apps really don’t exit unless you force kill them or they crash.  Apple wants them to suspend into the background.