Skip to content

Instantly share code, notes, and snippets.

@gcbrueckmann
Created August 6, 2016 13:13
Show Gist options
  • Save gcbrueckmann/b7c48a3ef5525ef6c0af14b006c4d30f to your computer and use it in GitHub Desktop.
Save gcbrueckmann/b7c48a3ef5525ef6c0af14b006c4d30f to your computer and use it in GitHub Desktop.
Determine the actual home directory URL from a sandboxed app
extension NSURL {
/// A URL identifying the user's home directory, typically in `/Users`.
///
/// Calling `NSHomeDirectory()` from a sandboxed app will return an descendant of the app sandbox.
/// This property on the other hand will return the actual home directory outside the sandbox.
/// Note that access restrictions still apply, e.g. testing access to the returned URL will normally fail.
static var actualHomeDirectoryURL: NSURL? {
let pw = getpwuid(getuid())
return NSURL(fileURLWithFileSystemRepresentation: pw.memory.pw_dir, isDirectory: true, relativeToURL: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment