Getting path to users Library folder in OS X
Asked Answered
A

3

8

I need to open a NSSavePanel with the users Library folder as destination folder. Normally I would do this by entering ~/Library/ in [NSSavePanel beginSheetForDirectory].

This works fine as long as the application is not sandboxed. For sandboxed applications this will result in the NSSavePanel trying to access a folder inside the applications document "box".

I cannot refer to /Users/username/Library/ as I do not know the users username at runtime. So how do I link to this path in cocoa?

Anselma answered 3/11, 2011 at 10:26 Comment(1)
If all you want to do is find the user's home dir see #6958948Klotz
W
2

Not sure if this will work on a sandboxed application but this is how I do it right now. This will return /User/TheirUserName

-(NSString *)homeDirectory
{
    return NSHomeDirectory();
}
Wb answered 3/11, 2011 at 22:11 Comment(1)
NSHomeDirectory() returns in-sandbox path, at least on 10.7.4. However, while this is the proper method for getting a HOME folder, this is the improper way to fetch the LIBRARY folder. What if the path changes? Please use JWWalker's solution of NSSearchPathForDirectoriesInDomains(). Iterate over all returned elements whenever possible.Nipha
C
12

I'm not sure how sandboxing fits in with this, but you can find the user's library directory using:

NSArray* paths = NSSearchPathForDirectoriesInDomains( NSLibraryDirectory, NSUserDomainMask, YES );
Cerberus answered 3/11, 2011 at 22:39 Comment(0)
W
2

Not sure if this will work on a sandboxed application but this is how I do it right now. This will return /User/TheirUserName

-(NSString *)homeDirectory
{
    return NSHomeDirectory();
}
Wb answered 3/11, 2011 at 22:11 Comment(1)
NSHomeDirectory() returns in-sandbox path, at least on 10.7.4. However, while this is the proper method for getting a HOME folder, this is the improper way to fetch the LIBRARY folder. What if the path changes? Please use JWWalker's solution of NSSearchPathForDirectoriesInDomains(). Iterate over all returned elements whenever possible.Nipha
B
0

It depends what you are trying to achieve.

  • If the behavior is required by your application, then you can request a temporary exception entitlement when submitting the application to the Mac App Store. But sooner or later, you will have to find a solution to remove this exception.
  • If you want to access data that were previously stored in the ~/Library/ folder, you can define a migration strategy to move back the data into the sandbox.
Blitzkrieg answered 3/11, 2011 at 18:11 Comment(1)
I do not need any elevated entitlements to achive this. It is enough to have read/write on the file system, as I use a save dialog to do the actual saving. I have run a proof of concept on this and know that works. What I need the user to do, is to save some files in this folder. That is why I need the actual folder and not just access to it's content. So it is no good to bring the data into my sandbox either. So I need some kind of way to read the users "username" to be able to build up the path to his Library folder, like: "~/user/[username]/Library/Priceless

© 2022 - 2024 — McMap. All rights reserved.