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

谁说我不能喝 提交于 2021-02-20 06:54:50

问题


I am struggling to simply create a folder in the users download directory on macos with this code:

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)
    }
}

This is the output:

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

How can I fix this?


回答1:


Xcode 9 and later enables sandboxing by default, which severely limits interactions with the system. However you can still write to the Downloads folder by adding an entitlement to your app:

Select your target, then Capabilities and set Downloads Folder to Read/Write:

If you have no intention of distributing your app through the Mac App Store, you can turn off sandboxing. Your application can still be signed but users will get a warning when they first launch your app.



来源:https://stackoverflow.com/questions/50200377/how-to-create-a-directory-in-downloads-folder-with-swift-on-macos-permission-ex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!