Mac App Store - 无法保存文件
通过 Mac App Store 分发应用程序时,保存文件是否有任何限制?
如果我从我的计算机编译并运行我的应用程序,它可以正常工作 - 它可以保存配置。
但是,通过 Mac App Store 下载的版本不会保存配置。我什至没有看到配置文件。有人知道发生了什么事吗?
这是保存配置的代码:
-(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);
}
}
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.
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?
This is the code that saves the config:
-(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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的目标是 Mac App Store,则无法将文件写入应用程序包目录。该捆绑包应该是不可变的。
考虑使用 NSUserDefaults 保存配置,或者,如果您确实需要单独的文件,则使用 官方推荐位置是(~)/Library/Application Support。 Matt Gallagher 写了一篇很好的文章,名为 查找或创建应用程序支持目录< /a> 其中他提供了一个使用标准
NSApplicationSupportDirectory
后跟可执行文件名称的解决方案。You cannot write files to the application bundle directory if you’re targeting the Mac App Store. The bundle is supposed to be immutable.
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 standardNSApplicationSupportDirectory
followed by the executable name.通常,您应该假设应用程序的资产是只读的。一般而言都是如此,而不仅仅是应用程序商店。
如果要将用户设置保存为属性列表,请使用 NSUserDefaults 而不是修改应用程序内的文件。这将“只做正确的事”,即将首选项保存到
~/Library/Preferences
。Generally, you should assume that your application's assets are read-only. This is true in general, not just for the app store.
If you want to save user settings as a property list, use
NSUserDefaults
instead of modifying files inside the application. This will "just do the right thing", which is to save preferences to~/Library/Preferences
.