macOS Swift-App写入权限问题?

时间:2019-06-26 12:08:50

标签: swift macos file permissions macos-mojave

首先我要说:

  • 关闭沙箱
  • 我确实为应用程序提供了Mojave的完整磁盘访问权限
  • 我导出了该文件,并选择对其进行签名以进行手动分发(没有App Store)

问题是,我尝试通过FileManager.default.createFile在/ Library / Application支持中创建一个文件,该文件在我的主文件夹中有效,例如/ Users / username / Library,因此这不应该是编程问题。 / p>

但是我似乎没有写/ Library的权限...如何授予我的应用这些权限?

感谢所有帮助。

谢谢!

1 个答案:

答案 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")
    }
}