首先我要说:
问题是,我尝试通过FileManager.default.createFile在/ Library / Application支持中创建一个文件,该文件在我的主文件夹中有效,例如/ Users / username / Library,因此这不应该是编程问题。 / p>
但是我似乎没有写/ Library的权限...如何授予我的应用这些权限?
感谢所有帮助。
谢谢!
答案 0 :(得分:0)
您可以尝试打开NSOpenPanel并明确获取该文件夹的权限。这是一些入门的代码。
public static func allow(folder: String, prompt: String, callback: @escaping (URL?) -> ())
{
let openPanel = NSOpenPanel()
openPanel.directoryURL = URL(string: folder)
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = true
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = false
openPanel.prompt = prompt
openPanel.beginSheetModal(for: self.window!) // use the window from your ViewController
{
result in
if result.rawValue == NSFileHandlingPanelOKButton
{
if let url = openPanel.url
{
self.store(url: url) // Store for later use.
}
}
}
}
public static func store(url: URL)
{
guard let path = self.path else { return }
do
{
let data = try url.bookmarkData(options: NSURL.BookmarkCreationOptions.withSecurityScope, includingResourceValuesForKeys: nil, relativeTo: nil)
NSKeyedArchiver.archiveRootObject(self.folders, toFile: path)
}
catch
{
Swift.print("Error storing bookmarks")
}
}