Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the users home directory in a sandboxed app?

NSHomeDirectory() is retuning my sandbox root, not my home directory. [@"~" stringByExpandingTildeInPath] is doing the same thing.

This /Users/username/Library/Containers/appID/Data is what's being returned. How do I get /Users/username/?

like image 348
Justin808 Avatar asked Mar 04 '12 08:03

Justin808


People also ask

What is sandboxed application data?

Android sandboxThe Android platform isolates apps from each other and protects them -- and the overall system -- from malicious apps and intruders. Android assigns a unique user ID (UID) to each application to create a kernel-level sandbox. This kernel ensures security between apps and the system at the process level.

Are Apple apps sandboxed?

All third-party apps are “sandboxed,” so they are restricted from accessing files stored by other apps or from making changes to the device. Sandboxing is designed to prevent apps from gathering or modifying information stored by other apps.


2 Answers

If you want the path to the user's real home directory you can use:

char *realHome = getpwuid(getuid())->pw_dir; 

Full example:

#include <unistd.h> #include <sys/types.h> #include <pwd.h> #include <assert.h>  NSString *RealHomeDirectory() {     struct passwd *pw = getpwuid(getuid());     assert(pw);     return [NSString stringWithUTF8String:pw->pw_dir]; } 

This gives you the path to the user's home, but does not automatically give you access to that folder. As noted in comments, you can use this path for:

  • providing a sane default folder for the open/save dialogs
  • detecting whether you are in a sandbox, by comparing the result to NSHomeDirectory()
like image 152
CRD Avatar answered Oct 14 '22 14:10

CRD


From apple documentation:

Accessing User Data

Mac OS X path-finding APIs, above the POSIX layer, return paths relative to the container instead of relative to the user’s home directory. If your app, before you sandbox it, accesses locations in the user’s actual home directory (~) and you are using Cocoa or Core Foundation APIs, then, after you enable sandboxing, your path-finding code automatically uses your app’s container instead.

For first launch of your sandboxed app, Mac OS X automatically migrates your app’s main preferences file. If your app uses additional support files, perform a one-time migration of those files to the container, as described in “Migrating an App to a Sandbox.”

If you are using a POSIX command such as getpwuid to obtain the path to the user’s actual home directory, consider instead using a Cocoa or Core Foundation symbol such as the NSHomeDirectory function. By using Cocoa or Core Foundation, you support the App Sandbox restriction against directly accessing the user’s home directory.

If your app requires access to the user’s home directory in order to function, let Apple know about your needs using the Apple bug reporting system.

like image 24
Parag Bafna Avatar answered Oct 14 '22 14:10

Parag Bafna