[英]Xcode Swift MacOS App, drag and drop file into NSTextField
[英]Xcode Swift 3.0 macOS (Sierra) app unable to create file, no permission
我是Xcode和Swift的新手。 我试图在我的Documents目录中创建一个名为“ file.txt”的文件,并收到错误消息“您无权保存该文件”。
最终,我不想使用默认的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)
}
}
日志显示:
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”.
我在这里找到了答案,所以我想我会帮忙的。 问题是沙箱的局限性。 如果添加
com.apple.security.temporary-exception.files.home-relative-path.read-write
以包含要观看的父文件夹的字符串的数组的形式到达目标的权利文件。 就我而言,我只是添加了“ /”来观看所有内容。 这是我的:
<key>com.apple.security.temporary-exception.files.home-relative-path.read-write</key>
<array>
<string>/</string>
</array>
这将允许您访问与提到的文件夹相关的所有内容。
一个警告:如果(在其他应用程序中)没有其他FIFinderSyncController的设置,则似乎(未进行彻底测试)它们会相互影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.