NSUserDefaults 和 Lion 下的沙箱
根据 Apple 的要求,将我的项目之一移至支持沙箱。我像这样使用 UserDefaults:
[[NSUserDefaults standardUserDefaults] setObject:@(myNumber) forKey:myNumberKey];
[[NSUserDefaults standardUserDefaults] synchronize];
一切都按预期工作,直到我启用沙箱。如果我启用了沙箱,应用程序会在其沙箱目录(.plist.lockfile)中创建一个锁定文件,并且不会创建实际的.plist 文件。
我做错了什么以及如何将我的设置存储在沙盒环境下的 UserDefaults 中?
更新:在单独的 Mac 上安装了最新的 10.7.3 和最新的 Xcode - 同一项目在启用沙箱的情况下可以编译并正常工作。
另外,我尝试在我的 Mac 上运行这个项目,其中沙箱无法在不同的用户帐户(新创建的)下工作,但结果完全相同 - 不行。此时我认为问题出在这台特定 mac 上的系统配置上。但问题是 - 可以安全地假设我是唯一一个遇到这个奇怪问题的人吗?可能不是...
Moving one of my projects to support sandboxing as per Apple's requirements. I use UserDefaults like so:
[[NSUserDefaults standardUserDefaults] setObject:@(myNumber) forKey:myNumberKey];
[[NSUserDefaults standardUserDefaults] synchronize];
Everything works as expected until I enable sandboxing. If I have sandboxing enabled the app creates a lock file in it's sandbox directory ( .plist.lockfile) and doesn't create an actual .plist file.
What am I doing wrong and how do I store my settings in UserDefaults under sandbox environment?
Update: Installed fresh 10.7.3 with the latest Xcode on a separate Mac - the same project compiles and works fine with sandboxing enabled.
Also I've tried to run this project on my Mac where the sandbox doesn't work under different user account (freshly created) with exactly the same results - no go. At this point I think the problem is with system configuration on this particular mac. Question is though - is it safe to assume that I'm the only one with this weird problem? Probably not...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那应该可以正常工作。如果首选项系统能够创建锁定文件,则意味着您的应用程序具有在该目录中创建文件的适当权限,并且已正确查找应放置它们的位置。因此,肯定还有其他地方出了问题。
发生这种情况时是否有任何控制台日志记录? -synchronize的返回值是多少?
(旁白:一般来说 -synchronize 是不必要的,只会让你的应用程序变慢,NSUserDefaults 会自行处理)
That should work fine. If the preferences system is able to create the lock file, that means your app has appropriate privileges to create files in that directory and has correctly looked up the location where it should put them. Therefore, something else must be going wrong.
Is there any Console logging when this occurs? What's the return value of -synchronize?
(aside: in general -synchronize is not necessary and will just make your app slower, NSUserDefaults will handle that itself)