如何在沙箱应用程序中获取目标c中的用户主目录? [英] How do I get the users home directory in objective-c in a sandboxed app?
问题描述
NSHomeDirectory()
正在重新启动我的沙箱根,而不是我的主目录。 [@〜stringByExpandingTildeInPath]
正在做同样的事情。
NSHomeDirectory()
is retuning my sandbox root, not my home directory. [@"~" stringByExpandingTildeInPath]
is doing the same thing.
/ username / Library / Containers / appID / Data 是返回的内容。如何获得 / Users / username /
?
This /Users/username/Library/Containers/appID/Data
is what's being returned. How do I get /Users/username/
?
推荐答案
想要到用户真正的主目录的路径,你可以使用:
If you want the path to the user's real home directory you can use:
char *realHome = getpwuid(getuid())->pw_dir;
完整示例:
#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:
- 为打开/保存对话框提供一个合理的默认文件夹
- 通过将结果与
NSHomeDirectory()
- providing a sane default folder for the open/save dialogs
- detecting whether you are in a sandbox, by comparing the result to
NSHomeDirectory()
这篇关于如何在沙箱应用程序中获取目标c中的用户主目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!