How to get user home directory path (Users/"user name") without knowing the username in Swift3

10,755

Solution 1

You can use FileManager property homeDirectoryForCurrentUser

let homeDirURL = FileManager.default.homeDirectoryForCurrentUser

If you need it to work with earlier OS versions than 10.12 you can use

let homeDirURL = URL(fileURLWithPath: NSHomeDirectory())

print(homeDirURL.path)

Solution 2

There should be an easier way but -- at worst -- this should work:

let filePath = NSString(string: "~").expandingTildeInPath
Share:
10,755

Related videos on Youtube

JonnyTombstone
Author by

JonnyTombstone

Updated on September 14, 2022

Comments

  • JonnyTombstone
    JonnyTombstone over 1 year

    I am making a func that edits a text file in the Users/johnDoe Dir.

    let filename = "random.txt"
    let filePath = "/Users/johnDoe"
    let replacementText = "random bits of text"
    do {
    
     try replacementText.write(toFile: filePath, atomically: true, encoding: .utf8)
    
    }catch let error as NSError {
    print(error: + error.localizedDescription)
    }
    

    But I want to be able to have the path universal. Something like

    let fileManager = FileManager.default
        let downloadsURL =  FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first! as NSURL
        let downloadsPath = downloadsURL.path
    

    but for the JohnDoe folder. I haven't been able to find any documentation on how to do this. The closest thing I could find mentioned using NSHomeDirectory(). And I am not sure how to use it in this context.

    when I try adding it like...

    let fileManager = FileManager.default
        let downloadsURL =  FileManager.default.urls(for: NSHomeDirectory, in: .userDomainMask).first! as NSURL
        let downloadsPath = downloadsURL.path
    

    I get an error:

    "Cannot Convert value of type 'String' to expected argument type 'FileManager.SearchPathDirectory'"

    I've tried it .NSHomeDirectory, .NSHomeDirectory(), NShomeDirectory, NShomeDirectory()

  • JonnyTombstone
    JonnyTombstone over 7 years
    Yeah it works but only for 10.12+ I need it to work with 10.9 Up I should have mentioned that in my post sorry
  • JonnyTombstone
    JonnyTombstone over 7 years
    You sir... I could kiss you write now =). very simple and works like a charm. I guess I was trying to over think it.
  • Ky -
    Ky - almost 6 years
    This doesn't add anything that isn't already in the accepted answer
  • Oleksii Nezhyborets
    Oleksii Nezhyborets almost 5 years
    This will return app's home folder instead of user's if sandbox
  • ios coder
    ios coder over 2 years
    This Answer is just for macOS, do we have home dir or something like that for iOS as well?
  • Leo Dabus
    Leo Dabus over 2 years
    @ioscoder There is no home folder for iOS. What you have is the documents directory
  • Leo Dabus
    Leo Dabus over 2 years
    @ioscoder for more info about which folder to use you should take some time and read File System Basics. If you would like to get a specific folder in your app check this post Getting list of files in documents folder