0

I'm trying to verify that a file is present in the Downloads folder of the user. I have already enabled access to the Downloads folder in the permissions. Here's what I tried so far:

        let path = "~/Downloads/Game_Folder/GameFile.obb"
        let hasFile = FileManager().fileExists(atPath: path)
        if hasFile {
            installationLabel.stringValue = "Game Files Found!"
        }
        else {
            installationLabel.stringValue = "Game Files Not Found"
        } 

It will always say "Game files not found" when testing. I haven't found a definitive solution for this in Swift 5 yet, so that's why I'm asking.

Thanks for the help!

ModernEra
  • 33
  • 4
  • That is not the correct way to access the Downloads folder. Remember, you are sandboxed, plus the "twiddle" character means nothing to the File Manager. – matt Jul 19 '20 at 01:25
  • How would I access the Downloads folder then? I have enabled Read/Write for the Downloads folder in "Signing and Capabilities" so I should have permission right? – ModernEra Jul 19 '20 at 01:28
  • 1
    You have to ask the File Manager where the Downloads folder is, and go from there. For example see https://stackoverflow.com/q/50200377/341994 – matt Jul 19 '20 at 02:01
  • Does this answer your question? [How to check if a file exists in the Documents directory in Swift?](https://stackoverflow.com/questions/24181699/how-to-check-if-a-file-exists-in-the-documents-directory-in-swift) – Willeke Jul 19 '20 at 09:01
  • Technically yes, but I figured out a way more elegant solution I believe. I posted it beneath the question. – ModernEra Jul 19 '20 at 09:21

1 Answers1

1

Figured it out. Here's what I put for Swift 5.

let path = NSString(string: "~/test-node.js").expandingTildeInPath
let fileDoesExist = FileManager.default.fileExists(atPath: path)

The "~/test-node.js" should be replaced with the path of the file you want to verify exists.

ModernEra
  • 33
  • 4