【问题标题】:CodenameOne - filesystem access problems on iOS for Library folderCodenameOne - iOS 上库文件夹的文件系统访问问题
【发布时间】:2021-08-01 14:37:11
【问题描述】:

我的 CodenameOne 应用正在 iOS 模拟器(iPad 8th iOS 14)上进行测试。

它通过这种方法在私有文件夹中写入一些文件:

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");
}
}

它适用于 CN 模拟器(写入 .cn1/ 文件夹内) 但在 iOS 上,异常被捕获。 Library 文件夹在 iOS 上至关重要。

下面是获取根路径的方法

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;
}

我的应用程序的 CN 版本必须将这些私有文件写入与 swift 版本相同的位置,即库。

有字符串操作,没有添加额外的'/',文件路径看起来合法。

所以字符串

file:///Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Documents/

被转换并且 getRootPath() 方法返回

file:///Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Library/

但也有例外。

此外,在尝试写入后的某个时间点,我在控制台输出中看到了一些我认为相关的内容:

Failed to create directory /Users/mac/Library/Developer/CoreSimulator/Devices/alphanumeric-string/data/Containers/Data/Application/another-alphanumeric-string/Documents/cn1storage/

这是什么?和我的问题有关吗?

CN 文件系统访问是否损坏或有缺陷?

我知道 io 访问权限是由 CN 编译器自动创建的,但它们是否有效?

那么如何解决我关于 Library 文件夹的问题?

【问题讨论】:

    标签: java ios exception ios-simulator codenameone


    【解决方案1】:

    cn1storage 打印输出只是意味着存储目录已经存在。

    获取库路径的方法是这样的:Getting an iOS application's "~/Library" path reliably

    您需要使用这种方法。我认为您认为文档和库位于完全相同的层次结构下的假设是不正确的。

    【讨论】:

    • 在 Apple 文档“文件系统基础知识”(图 1-1)中,文档和库似乎是同一个数据容器下的兄弟。即使每次启动应用程序时 ID 都会发生变化,但我每次都会获取路径,因此这两条路径应该像兄弟姐妹一样。但是我意识到,如果不使用代号中的本机代码,就不可能获得路径。没有代号方法。而且我认为异常不会随着该本地方法的路径而消失。这很可能是文件系统访问问题。你测试过吗?真的如你所说有效吗?
    • 我没有测试过,但我不明白为什么会有不同的行为。我们所做的只是调用 Apple 的文件系统管理器:github.com/codenameone/CodenameOne/blob/master/Ports/iOSPort/…
    • Almog 我认为您正在 Documents 中创建一个文件夹,这是允许的,也许它与“私有”的 Library 文件夹不同,也许我必须在 Library 中创建一个文件夹,因为顶层是被禁止的.否则我必须放弃对文件系统的直接访问并使用 Storage 类,因为 Library 是被禁止的,那么就会引发异常。
    • 我在那个文件夹中成功写入,问题隐藏在嵌套的 try/catch 语句中。所以它在同一层次结构下。顺便说一下......../Library/里面有很多CN1的东西(我列出了它的内容)
    猜你喜欢
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多