iOS:无法将文件保存到“应用程序支持”文件夹,但可以“文档” [英] iOS: Can't save file to 'Application Support' folder, but can to 'Documents'
问题描述
我可以将二进制文件下载并保存到'Documents'文件夹中,并且自定义名称完全正常。
I can download and save a binary file to the 'Documents' folder with a custom name perfectly fine.
如果我只是将URL更改为'Application Support '文件夹而不是'Documents'文件夹,它无法写入该URL,说它不存在。
If I just change the URL to the 'Application Support' folder instead of the 'Documents' folder, it fails to write to that URL saying it doesn't exist.
这是URL构造代码:
- ( NSURL * ) getSaveFolder
{
NSURL * appSupportDir = nil;
NSURL * appDirectory = nil;
NSArray * possibleURLs = [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSAllDomainsMask];
if ( [possibleURLs count] >= 1 )
{
appSupportDir = [possibleURLs objectAtIndex:0];
}
if ( appSupportDir != nil)
{
NSString * appBundleID = [[NSBundle mainBundle] bundleIdentifier];
appDirectory = [appSupportDir URLByAppendingPathComponent:appBundleID];
}
return appSupportDir;
}
以下是保存代码:
- ( void ) writeOutDataToFile:( NSData * )data
{
NSURL * finalURL = [self.rootPathURL URLByAppendingPathComponent:self.aFileName];
[data writeToURL:finalURL atomically:YES];
}
如果我改变NSArray to:
If I change the NSArray to:
NSArray * possibleURLs = [[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
然后保存罚款。
I我已经阅读了Apple Docs on File的内容并且无法解决这个问题 - 我缺少什么?
I've read the Apple Docs on File stuff and can't fix this - what am I missing?
推荐答案
与<$不同c $ c> Documents 目录,默认情况下,应用程序的沙箱中不存在 Application Support
目录。您需要先创建它才能使用它。
Unlike the Documents
directory, the Application Support
directory does not exist in the app's sandbox by default. You need to create it before you can use it.
获取目录引用的一种更简单的方法是:
And a much simpler way to get a reference to the directory is:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
NSString *appSupportDirectory = paths.firstObject;
这篇关于iOS:无法将文件保存到“应用程序支持”文件夹,但可以“文档”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!