如何测试我是否可以读取或写入目录?

问题描述 投票:0回答:1

我正在编写一个应用程序,当我完成它时它将被沙箱化。目前还不是。

该应用程序会将文件从一个位置复制到另一个位置。因为它当前没有沙盒,所以我可以预设目录并继续。

当它处于沙盒状态时,我将无法执行此操作,直到我手动选择两个位置。如果不选择这两个位置,代码将无法正常工作。

我找不到任何东西可以让我知道我是否可以继续。我可以检查某种状态吗?

swift macos file-handling sandbox
1个回答
0
投票

您可以尝试一些简单的事情,例如这个示例代码。

更改

File Access Type
中的
App Sandbox
并查看更改。

struct ContentView: View {
    let path = "/Users/THE-USER/Downloads/testFile.txt"
    
    var body: some View {
        Text(path).foregroundStyle(.blue)
        Text("FREAD: \(checkFile(FREAD))")
        Text("FWRITE: \(checkFile(FWRITE))")
        Text("O_RDONLY: \(checkFile(O_RDONLY))")
        Text("O_RDWR: \(checkFile(O_RDWR))")
    }
    
    func checkFile(_ flag: Int32) -> Int32 {
        let thePath = (path as NSString)
        return open(thePath.fileSystemRepresentation, flag)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.