![](/img/trans.png)
[英]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.