【发布时间】:2016-11-15 23:28:07
【问题描述】:
在 /Applications 中安装我们的软件后,我希望我们的最终用户能够看到他们在“关于”框中输入的序列号。由于该软件已为所有用户激活,我想我需要将该信息写入一个常见的首选项文件位置。但是 Apple 的文档说 CFPreferencesAppSynchronize 不能用于 kCFPreferencesAnyUser:
“请注意,如果您有 根权限”
所以当然下面的代码不起作用(尽管 CFPreferencesAppSynchronize 返回 true):
void WritePrefTest(void)
{
// Write it out
CFStringRef textKey = CFSTR("myTextKey");
CFStringRef applicationID = CFSTR("com.foo.bar");
CFStringRef textValue = CFSTR("text that should be written");
CFPreferencesSetValue(textKey, textValue, applicationID, kCFPreferencesAnyUser, kCFPreferencesCurrentHost);
Boolean wasSuccessful = CFPreferencesAppSynchronize(kCFPreferencesAnyUser);
assert(wasSuccessful);
// read it back in
CFStringRef prefText = (CFStringRef)CFPreferencesCopyAppValue(textKey, applicationID);
NSLog(@"The text from com.foo.bar == %@", (__bridge NSString *)prefText);
}
由于序列号的激活和输入发生在安装程序之外,我应该 1) 请求提升权限(以前从未这样做过),2) 在其他文件夹(/Library/Application Support)中创建一个 plist - 假设我可以在那里写 - 或者 3) 将它写到其他一些 macos 接受的文件夹中?
【问题讨论】:
标签: xcode macos cocoa installation