简体   繁体   English

Xcode Swift 3.0 macOS(Sierra)应用无法创建文件,没有权限

[英]Xcode Swift 3.0 macOS (Sierra) app unable to create file, no permission

I'm fairly new to Xcode and Swift. 我是Xcode和Swift的新手。 I'm trying to create a file called "file.txt" in my Documents directory and getting the error "You don't have permission to save the file." 我试图在我的Documents目录中创建一个名为“ file.txt”的文件,并收到错误消息“您无权保存该文件”。

Ultimately, I DO NOT want to use the default Documents directory as I'm using FIFinderSyncController to watch everything ("/"). 最终,我不想使用默认的Documents目录,因为我正在使用FIFinderSyncController监视所有内容(“ /”)。

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

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”.

I found the answer here so I thought I would help out. 我在这里找到了答案,所以我想我会帮忙的。 The issue is the limitations of sandbox. 问题是沙箱的局限性。 If you add 如果添加

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

To the entitlements file for the target as an Array with strings for the parent folders you want to watch. 以包含要观看的父文件夹的字符串的数组的形式到达目标的权利文件。 In my case I just added "/" to watch 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 relative to the folder mentioned. 这将允许您访问与提到的文件夹相关的所有内容。

One warning: it seems (not thoroughly tested) that if there are other FIFinderSyncController's set up (in other apps), they can effect each other. 一个警告:如果(在其他应用程序中)没有其他FIFinderSyncController的设置,则似乎(未进行彻底测试)它们会相互影响。

I had the same issue. 我遇到过同样的问题。 I solved it by setting the "App Sandbox" key to "No" in the "Entitlements File". 我通过在“权利文件”中将“应用程序沙箱”键设置为“否”来解决此问题。 Hope this will help. 希望这会有所帮助。

在此处输入图片说明

What worked for me in a similar case was to select Read/Write in "User Selected Files" in the Capabilities of the Sandbox. 在类似情况下对我有用的是在“沙箱”功能的“用户选择的文件”中选择“读/写”。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM