Mac App Store-无法保存文件 [英] Mac App Store - can't save a file
问题描述
在Mac App Store上分发应用程序时,保存文件方面是否有任何限制?
Are there any restrictions as far as saving files when you distribute an app over the Mac App Store?
如果我从计算机上编译并运行应用程序,则可以正常运行很好-保存配置。
If I compile and run my app from my computer it works fine - it saves the configuration.
但是,通过Mac App Store下载的版本未保存配置。我什至看不到配置文件。有人知道发生了什么吗?
However the version that was downloaded over the Mac App Store is not saving the configuration. I don't even see the config file. Anyone knows what is going on?
这是保存配置的代码:
-(void)saveConfig:(id)plist {
NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingString: CONFIG_FILE_NAME];
NSData *xmlData;
NSString *error;
xmlData = [NSPropertyListSerialization dataFromPropertyList: plist
format: NSPropertyListXMLFormat_v1_0
errorDescription: &error];
if(xmlData)
{
if (![xmlData writeToFile:path atomically:YES])
NSLog(@"Failed to write the config file onto the hard drive");
}
else
{
NSLog(error);
}
}
推荐答案
您如果您以Mac App Store为目标,则无法将文件写入应用程序捆绑目录。
You cannot write files to the application bundle directory if you’re targeting the Mac App Store. The bundle is supposed to be immutable.
请考虑使用 NSUserDefaults
保存您的配置,或者,如果您确实需要单独保存文件,官方推荐的位置是(〜)/图书馆/应用程序支持。马特·加拉格尔(Matt Gallagher)撰写了一篇不错的文章,名为查找或创建应用程序支持目录中,他提供了一种使用标准 NSApplicationSupportDirectory
后跟可执行文件名的解决方案。
Consider saving your configuration with NSUserDefaults
or, if you truly need a separate file, the officially recommended location is (~)/Library/Application Support. Matt Gallagher wrote a nice post called Finding or creating the application support directory in which he provides a solution that uses standard NSApplicationSupportDirectory
followed by the executable name.
这篇关于Mac App Store-无法保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!