如何在沙盒应用程序中获取用户的主目录? [英] How do I get the users home directory in a sandboxed app?

查看:98
本文介绍了如何在沙盒应用程序中获取用户的主目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSHomeDirectory()正在重新调整我的沙箱根目录,而不是我的主目录. [@"~" stringByExpandingTildeInPath]在做同样的事情.

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

/Users/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()

这篇关于如何在沙盒应用程序中获取用户的主目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆