Bug 16580 - Add binding for NSHomeDirectory (for App Store sandboxed apps)
Summary: Add binding for NSHomeDirectory (for App Store sandboxed apps)
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Library (Xamarin.Mac.dll) ()
Version: 1.6.19
Hardware: PC Mac OS
: --- enhancement
Target Milestone: ---
Assignee: Aaron Bockover [MSFT]
Depends on:
Reported: 2013-12-03 19:32 UTC by Brendan Zagaeski (Xamarin Team, assistant)
Modified: 2015-03-15 23:59 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Brendan Zagaeski (Xamarin Team, assistant) 2013-12-03 19:32:46 UTC
Reported on behalf of a few users.

When running in the App Store sandbox, apps have unrestricted read-write access to their own container directories, but not the rest of the file system. The Apple-documented way to get the path to this container directory is to use the NSHomeDirectory function [1]. At the moment, this method is not available in XamMac.dll. Perhaps it would be helpful to add it?

> [1] https://developer.apple.com/library/mac/documentation/security/conceptual/AppSandboxDesignGuide/AppSandboxInDepth/AppSandboxInDepth.html#//apple_ref/doc/uid/TP40011183-CH3-SW6

## Workaround
As mentioned on the related forum cases, it's not too difficult to work around this missing function. You can just add a P/Invoke for the method in your own code:

> [DllImport(MonoMac.Constants.FoundationLibrary)]
> public static extern IntPtr NSHomeDirectory();
> public static string ContainerDirectory
> {
>     get {
>         return ((NSString)Runtime.GetNSObject (NSHomeDirectory ())).ToString ();
>     }
> }

## Related forum cases
> http://forums.xamarin.com/discussion/10465/nshomedirectory-doesn-t-give-sandboxed-container-path-even-when-sandboxed
> http://forums.xamarin.com/discussion/2884/what-is-the-status-of-nshomedirectory-class
Comment 1 Miguel de Icaza [MSFT] 2014-12-03 14:53:39 UTC
This is now

Comment 2 troy-dawson 2015-03-15 23:59:21 UTC
I don't seen NSApplication.ContainerDirectory, but this:

	static string ContainerRoot
			return Directory.GetParent(NSFileManager.DefaultManager.GetUrls(NSSearchPathDirectory.LibraryDirectory, NSSearchPathDomain.User).First().Path).FullName;

is an alternative I guess.