Mac沙盒应用无法写入Documents目录权限问题求助
iOS沙盒Documents目录文件复制权限问题
我编写了如下函数,用于将文件复制到沙盒应用Documents目录下的指定文件夹:
extension FileManager { func copy(file: URL, to directory: Directory) throws -> URL { let documentsDirectory = URL.documentsDirectory let targetFolder = documentsDirectory .appendingPathComponent(directory.rawValue) if !fileExists(atPath: targetFolder.path()) { try createDirectory(at: targetFolder, withIntermediateDirectories: false) } let destinationURL = targetFolder .appendingPathComponent(file.lastPathComponent) try self.copyItem(at: file, to: destinationURL) return destinationURL } }
我尝试复制的文件是通过SwiftUI的fileImporter获取的:
.fileImporter(isPresented: $filePickerOpen, allowedContentTypes: [.item]) { result in guard case let .success(fileUrl) = result else { return } fm.copy(file: fileUrl, to: .someDir) }
但操作失败,提示我没有访问someDir的权限。令我困惑的是,someDir目录是在Documents文件夹中创建的,显然我的应用有权限访问自身的Documents目录(我提及这点是因为看到其他问题中提到这可能是随时间变化的权限相关bug)。
补充说明:
为了更清晰,copy函数位于FileManager扩展中,Directory是一个包含预定义目录名称的枚举。
问题排查与修复方案
修复URL路径调用错误
代码中targetFolder.path()是错误写法,Swift的URL路径属性是path(而非方法);同时建议将目录创建的withIntermediateDirectories设为true,确保多层目录能自动创建:if !fileExists(atPath: targetFolder.path) { try createDirectory(at: targetFolder, withIntermediateDirectories: true) }处理安全范围URL的访问权限
fileImporter返回的是安全范围URL,必须显式请求访问权限才能操作:.fileImporter(isPresented: $filePickerOpen, allowedContentTypes: [.item]) { result in guard case let .success(fileUrl) = result else { return } guard fileUrl.startAccessingSecurityScopedResource() else { print("无法获取文件访问权限") return } defer { fileUrl.stopAccessingSecurityScopedResource() } do { try fm.copy(file: fileUrl, to: .someDir) } catch { print("复制失败:\(error.localizedDescription)") } }捕获详细错误信息
在调用copy方法时添加do-catch块,打印完整错误信息,精准定位问题:do { let destUrl = try fm.copy(file: fileUrl, to: .someDir) print("文件复制成功:\(destUrl)") } catch { print("复制错误详情:\(error)") }检查Directory枚举的rawValue
确认.someDir对应的rawValue是合法的目录名称,无特殊字符或系统保留路径。
内容的提问来源于stack exchange,提问作者Krisztian
相关产品推荐
相关产品推荐

