Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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.

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?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

kristiaand

macrumors member
Original poster
Dec 5, 2007
63
0
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.


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!!
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,566
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.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.