访问“私人文档”中的文件使用 iTunes 传输的文件夹

发布于 2024-09-27 03:45:04 字数 850 浏览 3 评论 0原文

目前,我正在尝试访问使用 3.2 引入的新 iOS 功能传输的文件。

- (NSString *)getPrivateDocsDir {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Private Documents"];

    NSError *error;
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];   

    return documentsDirectory;

}


 // and then in a method
 NSString *documentsDirectory = [self getPrivateDocsDir];
 NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];

问题是,这在模拟器中工作正常,但在我的 iPhone 上,files 数组为空。

有什么想法如何直接访问该目录吗?

Currently I'm trying to access the files that are transferred using the new iOS feature introduced with 3.2.

- (NSString *)getPrivateDocsDir {

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    documentsDirectory = [documentsDirectory stringByAppendingPathComponent:@"Private Documents"];

    NSError *error;
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error];   

    return documentsDirectory;

}


 // and then in a method
 NSString *documentsDirectory = [self getPrivateDocsDir];
 NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];

The problem is, this works fine in simulator, but on my iphone, the files array is empty.

Any ideas how to access this directory directly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你 2024-10-04 03:45:04

无法通过 iTunes 访问 Library-Directory,并且“私人文档”位于该库目录内。
顾名思义,私人文件是私人的。故意不通过 iTunes 访问它们。

如果您想访问文档目录(可通过 itunes 访问),请将其替换

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

为:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

编辑:
像这样访问文件。

- (NSString *)documentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

NSString *documentsDirectory = [self documentsDirectory];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];

The Library-Directory is not accessible through iTunes, and "Private Documents" is inside this library directory.
The private documents, are, as the name suggests, private. They are intentionally not accessible through iTunes.

If you want to access the documents directory (which is accessible through itunes) replace this

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);

with this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

EDIT:
Access the files like this.

- (NSString *)documentsDirectory {
    return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

NSString *documentsDirectory = [self documentsDirectory];
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文