Suitable Path for Application Data in Cocoa Application

Discussion in 'Mac Programming' started by ruhi, Jul 23, 2009.

  1. macrumors member

    Joined:
    Jun 17, 2009
    #1
    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
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    /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.

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

    Code:
    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: as this automatically handles adding a directory separator if required.
     
  3. thread starter macrumors member

    Joined:
    Jun 17, 2009
    #3
    Suitable Path for Application Data in Cocoa Application

    Thanks a lot!!!

    It worked..
    :)

    Thanks.
    Ruhi.
     
  4. macrumors 6502a

    Joined:
    Oct 13, 2007
    #4
    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.
     
  5. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Location:
    UK
    #5
    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?
     
  6. macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #6
    Not to mention MAC is an acronym, for something completely different :) (Media Access Control)
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    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!
     

Share This Page