iOS - writing in Library error - and cn1storage folder error

22 views
Skip to first unread message

P5music

unread,
May 9, 2021, 9:19:06 AM5/9/21
to CodenameOne Discussions
My app is being tested on the iOS simulator.

It writes some files in the private folder by means of this method:

    public void writeFile() throws IOException {
    try(OutputStream os = FileSystemStorage.getInstance().openOutputStream(Utils.getRootPath()+DATA_FILE);)
    {
    os.write(JSONText.getBytes("UTF-8"));
    os.flush();
    os.close();
    } catch(IOException err) {
    System.out.println("exception trying to write");
    }
    }

It works on the CN simulator (writes inside the .cn1/ folder)
but on iOS the exception is catched.

Below is the method to get the root path
public static String getRootPath()
{
String documentsRoot=FileSystemStorage.getInstance().getRoots()[0];
String os=Display.getInstance().getPlatformName();

if (os.toLowerCase().contains("ios")) {

int pos=documentsRoot.lastIndexOf("Documents");
if (pos==-1) return documentsRoot+"/";
String libraryRoot=documentsRoot.substring(0,pos)+"Library";
String result=libraryRoot+"/";

return result;
}

The CN version of my app has to write those private files in the same location as the swift version, that is Library.
So the string
file:///Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Documents/
is transformed and
the getRootPath() method returns
file:///Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Library/
But there is exception.

Furthermore, at some point after the writing attempt, I see in the console output something I think is relevant:
Failed to create directory /Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Documents/cn1storage/

What is this? Is it related to my problem? How to fix it?
Thanks in advance

Shai Almog

unread,
May 9, 2021, 10:10:42 PM5/9/21
to CodenameOne Discussions
By default we return directories with a / already there. It looks like you're adding an extra / and iOS is very sensitive to those things.

P5music

unread,
May 10, 2021, 2:09:55 AM5/10/21
to CodenameOne Discussions
As you can see the method returns the correct value
.../Library/
and the file name is appended, so the final result is a valid file path, like
.../Library/filename.ext
Indeed you can see that the root path undergoes a string manipulation, so there are no extra '/'.
Even if the root is like '.../Documents/', the string manipulation strips the entire final part before adding the replacement.

So what's the problem? Maybe default FileSystem permissions in the XCode project are not working in OS14? Something else?
Regards

Shai Almog

unread,
May 10, 2021, 11:18:35 PM5/10/21
to CodenameOne Discussions
All paths in Codename One must be absolute paths not relative paths so ../ wouldn't be valid.

P5music

unread,
May 11, 2021, 4:33:19 AM5/11/21
to CodenameOne Discussions
I think .../ wouldn't be valid too (three dots).

I am talking of string manipulation, that's only the final part of the string to focus your attention on the subject. But I failed as I can see.

I think my question deserve a more prfessional approach from you.

So
From now on, I put some questions on StackOverflow so they are taken seriously.


Regards
Reply all
Reply to author
Forward
0 new messages