application preferences?

Discussion in 'Mac Programming' started by kristiaand, Jan 11, 2008.

  1. kristiaand macrumors member

    Joined:
    Dec 5, 2007
    #1
    hi all, just wondering how i go about implementing software preferences so that when an app loads it reads them etc.

    is it possible to store this file in the application bundle rather than have it stored as a file somewhere else? or is it good practice to store it in the preferences folder?

    as normal im a bit of a newbie to cocoa so any advice you can give really appreciated.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    You can access preferences in Cocoa via the NSUserDefaults class.

    Prefs are stored automatically in the ~/Library/Preferences folder. I wouldn't try to put them anywhere else unless you're making a portable (as in able to take with you on a flash drive) version.
     
  3. kristiaand thread starter macrumors member

    Joined:
    Dec 5, 2007
    #3
    hi mate thanks for that, all i wanted was your standard preferences , like how lists are shown, toolbars etc??

    so i am assuming that i can use NSUserDefaults save any sort of preference for my app?
     
  4. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Pretty much. The types of data that can be stored are NSData, NSString, NSNumber, NSDate, NSArray, and NSDictionary. Anything else can be archived into an NSData object.
     
  5. kristiaand thread starter macrumors member

    Joined:
    Dec 5, 2007
    #5

    Hi Kainjow, thanks for the info mate, just been playing with an example from apples website, Cocoa keeps blowing my mind for how simple things can be done.....

    its a matter of ticking a box for the control, and naming it, and viola that control stores its info at the close of the program....

    thanks again!!
     
  6. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    You definitely shouldn't try to store preferences in the application bundle, and often you can't anyway. For several reasons: First: Your application could be stored on a server, and could be used by multiple users. How do you keep their preferences apart? Second: Your application could be stored on a CD-ROM, on a read-only disk image, or on a write-protected memory stick. How do you store the preferences? Third: At some point you might want to sign your application, so users can be sure it is your application, and not a trojan that attacks their Mac. If you modify the application folder by storing preferences, that won't work. Last: You can have more than one user on a Macintosh. Again, how do you keep their preferences apart? And anyone but the system administrator would need to enter a password anyway to store things into the application bundle.

    In Carbon, you would use the function "FSFindFolder" to find the proper location for the preferences folder - you can use it to find the preferences folder, document folder, music folder and dozens of other special folders without having to know their names, so your code will work on a French system as well, where the preferences folder might not be called "Preferences". In Cocoa, there are APIs specifically for preferences that will do the right thing anyway.
     

Share This Page