Swift FileManager 怎么申请文件权限?

我通过下面的方式获得一组文件的URL

let openPanel = NSOpenPanel()
openPanel.allowsMultipleSelection = true
openPanel.canChooseDirectories = false
if openPanel.runModal() == .OK {
    self.openedFiles = openPanel.urls;
}

然后对其进行重命名

let fileManager = FileManager.default
for index in 0..<openedFiles.count {
    let url: URL = openedFiles[index]
    let newFileName = getNewFileName(url: url)
    let newURL = URL(string: newFileName, relativeTo: url)!
    do {
        try FileManager.default.moveItem(at: url, to: newURL)
    } catch let error {
        dump(error)
    }
}

然后会提示没有权限

couldn’t be moved because you don’t have permission to access

这里的设置已经修改为 Read/Write

image.png

谢谢

阅读 4.9k
2 个回答

将 App Sandbox 设置为 No 可以了。

image.png

打开Sandbox;添加以下items
For User Selected File

  • com.apple.security.files.user-selected.read-only
  • com.apple.security.files.user-selected.read-write

For Downloads Folder

  • com.apple.security.files.downloads.read-write

EnablingAppSandbox

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏