簡體   English   中英

如何在macos上使用swift在下載文件夾中創建目錄?許可例外。

[英]How to create a directory in downloads folder with swift on macos? Permission exception.

我很難在macos上的用戶下載目錄中使用以下代碼創建一個文件夾:

static func createFolderInDownloadsDirectory() {
    let downloadsDirectory = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
    let downloadsDirectoryWithFolder = downloadsDirectory.appendingPathComponent("FolderToCreate")

    do {
        try FileManager.default.createDirectory(at: downloadsDirectoryWithFolder, withIntermediateDirectories: true, attributes: nil)
    } catch let error as NSError {
        print(error.localizedDescription)
    }
}

這是輸出:

You don’t have permission to save the file “FolderToCreate” in the folder “Downloads”. 

我怎樣才能解決這個問題?

Xcode 9及更高版本默認啟用沙盒,嚴重限制了與系統的交互。 但是,您仍然可以通過向應用添加權利來寫入“下載”文件夾:

選擇目標,然后選擇功能並將下載文件夾設置為讀/寫:

啟用App Sandbox下的“下載文件夾”的讀/寫

如果您無意通過Mac App Store分發應用程序,則可以關閉沙盒。 您的應用程序仍然可以簽名,但用戶在首次啟動您的應用時會收到警告。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM