PDA

View Full Version : Suitable Path for Application Data in Cocoa Application




ruhi
Jul 23, 2009, 11:55 PM
What is the path where Application data can be saved which is same for all users.

Like For windows it is All Users/Application data which is same for all users and accessible by all users.

what is the path in MAC which i can use for my cocoa application.

Thanks.
Ruhi



robbieduncan
Jul 24, 2009, 03:30 AM
/Library/Application Support/<your app name>/

Note you should not assume this: don't hard code it. As well covered in the documentation you should use NSSearchPathsForDirectoriesInDomains (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/doc/uid/20000055-SW5).

An example to get the system wide Application Support directory would be:


NSArray *dirs = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,NSLocalDomainMask,YES);
// Assume the first path is the only one and that at least one path is returned: you should check this and correctly handle error situations
NSString *pathToSystemApplicationSupport = (NSString *) [dirs objectAtIndex:0];


Note I typed this straight into this reply: I've not tested it at all.

Edit to add: note this gets /Library/Application Support. You need to append your app name to this. Use stringByAppendingPathComponent: (http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/stringByAppendingPathComponent:) as this automatically handles adding a directory separator if required.

ruhi
Jul 24, 2009, 03:54 AM
Thanks a lot!!!

It worked..
:)

Thanks.
Ruhi.

larkost
Jul 24, 2009, 02:42 PM
Note that /Library/Application Support is not necessarily a good place for this, as that folder does not allow non-admins to write to it. You could set up a folder for your application (say durring install time) that would have appropriate permissions, but if you try and set it up at run time if the first person is a non-admin it won't work.

And I think that the All Users folder in Vista has similar requirements, but that this was not enforced well enough in XP and before (gaping security hole).

Oh... and you might as well get used to Mac rather than MAC. Mac is short for Macintosh (so it technically should be "Mac."), not an acronym.

Eraserhead
Jul 24, 2009, 04:12 PM
Generally (wrt to preferences etc) you should save the data on a user specific basis so that different users can have different preferences.

Additionally computer programs these days should assume users aren't running as admin as running as admin is a security risk - this is especially an issue in corporate IT.

For what its worth what exactly do you need to save that requires it to be the same for all users of the computer?

Cinder6
Jul 24, 2009, 09:42 PM
Oh... and you might as well get used to Mac rather than MAC. Mac is short for Macintosh (so it technically should be "Mac."), not an acronym.

Not to mention MAC is an acronym, for something completely different :) (Media Access Control)

robbieduncan
Jul 25, 2009, 03:55 AM
Generally (wrt to preferences etc) you should save the data on a user specific basis so that different users can have different preferences.

Additionally computer programs these days should assume users aren't running as admin as running as admin is a security risk - this is especially an issue in corporate IT.

For what its worth what exactly do you need to save that requires it to be the same for all users of the computer?

I agree with all that, but there are reasons to save to /Library instead of ~/Library. For example when I still had enough time to work on Poster Paint I offered the ability to load any bitmap as the background for the image (like the checker-board Photoshop displays). When the user chose to add a background image I'd pop up a message asking if they wanted to add it for themselves only or for all users. If it was for all users then I used AuthenticationServices to get admin rights and ran an external executable as admin to copy the file. Amazingly complex for a very simple outcome!