CodenameOne - filesystem access problems on iOS for Library folder

Viewed 50

My CodenameOne app is being tested on the iOS simulator (iPad 8th iOS 14).

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. The Library folder is of paramount importance on iOS.

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.

There is string manipulation, and no extra '/' are added, the file path seems legit.

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?

Is CN filesystem access broken or flawed?

I know io access permissions are automatically created by the CN compiler, but are they working?

So how to fix my issue about the Library folder?

1 Answers

The cn1storage printout just means the storage directory already exists.

The way to get the library path is this: Getting an iOS application's "~/Library" path reliably

You need to use that approach. I think your assumption that Document and Library reside under the exact same hierarchy is just incorrect.

Related