2008-12-11 52 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,它負責爲您保留磁盤上的持久設置。