[英]macOS Swift-App write permission problem?
首先我必須說:
問題是,我嘗試通過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")
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.