2008-12-11 54 views

回答

14

在Mac OSX应用程序首选项是通过NSUserDefaults自动存储的,它将它们保存到.plist文件~/Library/Preferences/。你不需要对这个文件做任何事情,NSUserDefaults会为你处理所有事情。

如果您在基于非文档的应用程序(例如AddressBook.app)中有数据文件,则应将其存储在~/Library/Application Support/Your App Name/中。没有内置的方法来查找或创建此文件夹,您需要自己动手。这里有一个来自我自己的应用程序的例子,如果你看看一些Xcode项目模板,你会看到一个类似的方法。

+ (NSString *)applicationSupportFolder; 
{ 
    // Find this application's Application Support Folder, creating it if 
    // needed. 

    NSString *appName, *supportPath = nil; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); 

    if ([paths count] > 0) 
    { 
     appName = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleExecutable"]; 
     supportPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:appName]; 

     if (![[NSFileManager defaultManager] fileExistsAtPath:supportPath]) 
      if (![[NSFileManager defaultManager] createDirectoryAtPath:supportPath attributes:nil]) 
       supportPath = nil; 
    } 

    return supportPath; 
} 

请记住,如果你的应用程序是流行的,你可能会得到要求能够对不同的用户共享同一帐户的多个库文件。如果你想支持这个,约定是在应用程序开始按住alt/option键时提示一个使用路径。

0

对于大多数情况,您应该只使用NSUserDefaults API,它负责为您保留磁盘上的持久设置。