swiftmacos-mojavexcode10.1home-directory

why in xcode, home directory is showing as /Users/myname/Library/Containers/... and not the main folder?


I searched a lot and couldn't find the answer. I just started using xcode and have a very very simple program named "Image Test".

---> It has a button, and a textField. I want to show my system home directory path in the textField. This is my code:

@IBOutlet weak var textField1: NSTextField!
@IBAction func Button1(_ sender: Any) {

    do{
        let home = try FileManager.default.homeDirectoryForCurrentUser
        textField1.stringValue = home.path
    }
    catch{}
}

it works but the returned path is like: /Users/myname/Library/Containers/com.Image-Test/Data

I have tested this code before in Xcode Playground and it was correct. Its the same code but not throwing the right path.


Solution

  • It's because of sandboxing. The documentation states:

    The App Sandbox Container Directory

    It is located at a system-defined path, within the user’s home directory. In a sandboxed app, this path is returned when your app calls the NSHomeDirectory function.

    You can verify this by removing the App Sandbox capability (note: I am not suggesting that this is what you should do for your app).

    PS: If you intend to submit your app to the AppStore, sandboxing must be enabled.