1

what I've done is setup the com.apple.security.files.downloads.read-write to true, and have looked up the Apple Sandbox related docs, but I can't figure out how to get the downloads folder path, what I get is still container path like this: /Users/username/Library/Containers/com.errpro.Snell/Data/Downloads , the method I use is NSSearchPathForDirectoriesInDomains. I've seen someone use getpwent to get the path, but seems not appropriate. Any help would be appreciated.

oxnz
  • 835
  • 6
  • 16
  • If you are having issues with some code you wrote, then update your question with the code and point out what issues you having. – rmaddy Jan 13 '16 at 03:56
  • Why do you find `getpwent` (or `getpwuid`) inappropriate? It is a [valid route](http://stackoverflow.com/questions/9553390/how-do-i-get-the-users-home-directory-in-objective-c-in-a-sandboxed-app) to a solution. – CRD Jan 13 '16 at 05:17
  • @CRD, that's true, it's a valid solution to retrieve the user's home dir. but how to get the downloads dir? what if the user's home is not in a regular path, and downloads dir not named 'Downloads'? – oxnz Jan 21 '16 at 09:26
  • If you're thinking about language issues in regard to the naming there is an API to handle that. However the `URLForDirectory` is a better solution in this case. Was just curious at your "seems not appropriate" without any further explanation. – CRD Jan 21 '16 at 19:11

2 Answers2

4

You should use the method URLForDirectory. This method find the name of the current user and insert it in the URL path.

NSFileManager       *fm = [NSFileManager defaultManager];
NSURL               *downloadsURL;  

downloadsURL = [fm URLForDirectory:NSDownloadsDirectory
                   inDomain:NSUserDomainMask appropriateForURL:nil
                   create:YES error:nil];
Lionel_A
  • 550
  • 4
  • 12
  • sorry, you answer seems still not work in the sandboxed env, cause the result is file:///Users/username/Library/Containers/com.errpro.prodname/Data/Downloads/, but `/Users/username/Downloads` is what I need. – oxnz Jan 19 '16 at 03:49
  • 1
    /Users/username/Library/Containers/com.errpro.prodname/Data/Downloads is an alias that points to /Users/username/Downloads. It is transparent for your application – Lionel_A Jan 19 '16 at 07:53
  • I tried it again, and you're right. The downloads dir is a symbol link to the current user's downloads dir. And is this the effect of the 'com.apple.security.files.downloads.read-write' config? – oxnz Jan 21 '16 at 09:23
3

Swift 5

do {
     let url = try FileManager.default.url(for: FileManager.SearchPathDirectory.downloadsDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: true)
} catch{
      print(error)
}
DNG
  • 599
  • 4
  • 14