1

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?

P5music
  • 3,197
  • 2
  • 32
  • 81

1 Answers1

0

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.

Shai Almog
  • 51,749
  • 5
  • 35
  • 65
  • At Apple documentation "File System Basics" (fig 1-1) it seems that Documents and Library are siblings under the same data container. Even if the ID is changing every time the app is launched, I get the path every time so the two paths should be like siblings. However I realize it is not possible to get the path without resorting to native code in codenameone. No codenameone method for that. And I think the exception does not go away with the path from that native method. It is likely to be a filesystem access issue. Have you tested it? Does it work for sure as you say? – P5music May 12 '21 at 08:33
  • I haven't tested it but I don't see why this will act differently. All we do is invoke the file system manager from Apple: https://github.com/codenameone/CodenameOne/blob/master/Ports/iOSPort/nativeSources/IOSNative.m#L1608-L1619 – Shai Almog May 13 '21 at 03:02
  • AlmogI think you are creating a folder inside Documents, that is allowed, maybe it's different from the Library folder that is "private", maybe I have to create a folder inside Library because the top level is forbidden. Otherwise I have to surrender direct filesystem access and use the Storage classes, because Library is forbidden that way, then the exception is raised. – P5music May 13 '21 at 06:53
  • I succeded in writing in that folder, the problem was hidden in nested try/catch statements. So it is under the same hierarchy. By the way in ......../Library/ there is a lot of CN1 stuff (I listed its content) – P5music May 16 '21 at 18:54