Search code examples
cocoasandbox

Mac App Sandbox - NSHomeDirectory() issues


I just sandboxed my Mac App and in my app I save images in the documents folder. I have this file URL: file:///Users/myUser/Documents/myApp/Images/logo.jpg

After my sandbox I end up using NSHomeDirectory() instead of going directly to the Documents folder. The problem with this is that it strips out the file://. Basically, how do I append file:// to the URL?

This is what NSHomeDirectory() gives me:

/Users/myUser/Library/Containers/com.company.myApp/Data/myApp/Images/logo.jpg

Later on, the user can choose another directory and then I can use the power box, but before the user does that I need to be able to read/write files to the home directory.

Any ideas?


Solution

  • NSHomeDirectory() returns an NSString, not an NSURL. You can convert it into an NSURL by calling:

    NSURL* url = [NSURL fileURLWithPath:NSHomeDirectory()];
    

    See here for docs.

    You may also want to use NSHomDirectoryForUser(userName) instead of NSHomeDirectory().