1
votes

I'm using Lua on iOS and I'm having problems to open a file with io.open("filename.txt","w"), always getting "Permission denied" error. I know Lua requires the entire path of a file when reading and writing, but mobile operating systems such as iOS obscure the file system via "sandboxing." I do solve this?

Note: I'm not using Corona SDK.

2

2 Answers

1
votes

File paths will be relative to the correct sandboxed folder as long as you get them via system.pathForFile("file.txt", directory_enum), with directory_enum being system.DocumentsDirectory, system.ResourceDirectory, etc.

Otherwise, I just had the exact same "permission denied" problem, even though I AM using Corona. So the problem was that before the io.open call that returned an error, I had another io.open call, that was meant to read the same file, but the file handle wasn't closed after that. So, to put it simply:

io.open(path, "r")
...
io.open(path, "w") --ERROR! this one returns a nil handle and a "permission denied"!

To fix it do the following:

local fh = io.open(path, "r")
...
io.close(fh) --close the file after finishing with it
...
io.open(path, "w") --now the file is closed and can be opened again
0
votes

In Lua: You must specify the
- system.DocumentsDirectory,
- system.TemporaryDirectory, or
- system.CachesDirectory
parameter in the system.pathForFile() function when opening the file for writing. See io.open().

See object:write() in the Corona Docs

In iOS the user data is generally stored in the Documents directory.

On iOS no app has root access, this is a security feature and enforced by sandboxing.

Note: I know nothing about Corona/Lua, a simple Google of "Lua ios documents directory" found the above doc. Google first, ask second.