swift - macOS Swift-App 写权限问题?
问题描述
首先我要说:
- 沙盒已关闭
- 我确实为 Mojave 的应用程序提供了全盘访问权限
- 我将其导出并选择对其进行签名以进行手动分发(没有 App Store)
问题是,我尝试通过 FileManager.default.createFile 在 /Library/Application 支持中创建一个文件,该文件在我的主文件夹中有效,例如 /Users/username/Library,所以它不应该是一个编程问题。
但我似乎没有写入 /Library 的权限...如何授予我的应用程序这些权限?
感谢所有帮助。
谢谢!
解决方案
您可以尝试打开 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")
}
}
推荐阅读
- r - 较旧的 Mac 与较新的 Windows - R 真的无法分配足够的内存吗?
- c# - 正则表达式:匹配表达式周围最近的打开和关闭花括号内的所有内容
- javascript - Highcharts 同步图表与下拉选择而不是图例
- java - 通过 HTTP 请求发送大文件
- asp.net-mvc - ASP.NET MVC + Angular 6 中的一次调用主要,多次响应
- ruby - 使用 Time.now.between? 查看今天是否在两个日期期间之间
- python - python浓缩重复代码和子图
- django - 如何在使用 Python Requests 库获得的结果中提取特定数据
- swift - 如何在 MacOS 中显示 NSSavePanel?
- onedrive - 如何检查谁在锁定我的 OneDrive 共享文件以进行编辑?