Xcode Swift 3.0 macOS (Sierra) app cannot create file, without permission

I am new to Xcode and Swift. I am trying to create a file called "file.txt" in my Documents folder and get the error "You do not have permission to save the file."

Ultimately, I do NOT want to use the default Docs directory as I am using FIFinderSyncController to view everything ("/").

    let targetFile = (FIFinderSyncController.default().targetedURL()?.path)! + "/file.txt"

    NSLog("%@", targetFile as NSString)

    let fileManager = FileManager.default
    if fileManager.fileExists( atPath: (targetFile) ) == false {

        do {
            let targetString = (FIFinderSyncController.default().targetedURL()?.path)! + "/Untitled.txt"

            NSLog("%@", targetString as NSString)

            let fileManager = FileManager.default

            if fileManager.fileExists( atPath: targetString ) == false {
                let content = "" // Just creating empty file.

                //writing
                try content.write(toFile: targetString, atomically: false, encoding: String.Encoding.utf8)

                //reading
                let readingText = try NSString(contentsOfFile: targetString, encoding: String.Encoding.utf8.rawValue)

                NSLog("%@", readingText as NSString)
            }
        }
        catch {
            print(error.localizedDescription)
        }
    }

      

The log shows:

2017-04-06 13:35:46.450077-0700 Open New Text File[5177:1035580]     /Users/david/Documents/file.txt
2017-04-06 13:35:46.450257-0700 Open New Text File[5177:1035580]     /Users/david/Documents/file.txt
You don’t have permission to save the file "file.txt" in the folder "Documents".

      

+5


source to share


3 answers


I found the answer here, so I thought I'd help. The problem lies in the limitations of the sandbox. If you add

com.apple.security.temporary-exception.files.home-relative-path.read-write

      

Into the file permissions for the target as an array with strings for the parent folders you want to look at. In my case, I just added "/" to see everything. Here's mine:



<key>com.apple.security.temporary-exception.files.home-relative-path.read-write</key>
<array>
    <string>/</string>
</array>

      

This will allow you to access everything related to the specified folder.

One caveat: it seems (not fully tested) that if there are other FIFinderSyncController settings (in other applications) they might interfere with each other.

+5


source


I had the same problem. I solved this by setting the "Application Sandbox" key to "No" in the "Rights File". Hope this helps.



enter image description here

0


source


What worked for me in a similar case was to select Read / Write under User Selected Files under Sandbox Capabilities.

enter image description here

0


source







All Articles