如何将文件写入安全应用容器中的文件夹?

时间:2019-11-28 12:31:17

标签: macos appstore-sandbox

所以我有一个在沙箱中运行的MacOS应用。

仅当我使用简单的字符串和消息NSData writeToFile:options:error时,才能将文件保存到Application文件夹中。

但是...如果我尝试指定一个文件夹,它将收到以下错误消息:

 Error Domain=NSCocoaErrorDomain 
 Code=4 "The file “2C592751-5CA7-437C-B556-438045F7F78C” doesn’t exist." 
 UserInfo={NSFilePath=file:///Users/my-user-name/Library/Containers/com.myApp.SampleName/Data/Library/Application%20Support/SampleName/OfflineData/Notes/2C592751-5CA7-437C-B556-438045F7F78C, 
 NSUnderlyingError=0x600000ca1350 
 {Error Domain=NSPOSIXErrorDomain 
 Code=2 "No such file or directory"}}

我尝试以下方式

// THIS WORKS...
if (![xmlData writeToFile:uuidAsString
// WOULD RATHER USE THIS
if (![xmlData writeToFile:[pathAndFileURL stringValue] 
// OR THIS
if (![xmlData writeToFile:pathAndFileAsString

此文件的位置...

NSString *uuidAsString = [[obj uuid] stringValue];

NSURL *pathAndFileURL = [[NSFileManager defaultManager] offlineDataURLInContainer]
                    URLByAppendingPathComponent:fileNameString
                                 isDirectory:false] ;

NSString *pathAndFileAsString = [NSString stringWithFormat:@"OfflineData/Notes/%@", [[note uuid] stringValue]];

我在做什么错了?

更新:

我想我有一个线索!

当我尝试...

 NSString *embeddedPathInString = [NSString stringWithFormat:@"/Users/my-user-name/Library/Containers/com.myApp.SampleName/Data/Library/Application Support/SampleName/OfflineData/Notes/%@", [[note uuid] stringValue]];

 if (![xmlData writeToFile:pathAndFileAsString

有效!

现在我必须从NSURL获得正确的表示形式...

更新2:

[myNSURL path]

有效!

:)

0 个答案:

没有答案