移动文档文件夹的路径?

时间:2012-11-29 21:44:36

标签: iphone objective-c ios core-data icloud

要访问我的应用程序文档文件夹,我使用以下代码:

[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

我想访问此文件夹,但是:

~/Library/Mobile Documents

我如何轻松地将其作为路径值进行访问?我能以类似的方式做到这一点吗?

2 个答案:

答案 0 :(得分:1)

使用常量访问系统提供的目录的好处是,如果Apple决定更改结构,您的应用程序仍然可以工作。像~/Library/Mobile Documents这样的硬编码是脆弱的。

但是,您可以使用NSSearchPathForDirectoriesInDomain NSLibraryDirectory常量来访问Library目录。然后,您应该只追加Mobile Documents目录路径。

// Set the NO to YES to get the full path, not the ~ version.
NSString *path = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, NO) lastObject]; 
path = [path stringByAppendingPathComponent:@"Mobile Documents"];

查看http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html#//apple_ref/doc/c_ref/NSSearchPathDirectory中的常量值,似乎Mobile Documents目录没有特定的常量,因此硬编码方法可能是您唯一的选择。

答案 1 :(得分:1)

移动文档是iCloud文档。所以你想在iCloud中存储文件。

在OS X上,它们肯定在〜/ Library / Mobile Documents(10.7和10.8)中,但在iOS上你不应该看。

 "All documents of an application are stored either in the local sandbox or in an iCloud container directory."...

  "A user should not be able to select individual documents for storage in iCloud. "

http://developer.apple.com/library/ios/#documentation/DataManagement/Conceptual/DocumentBasedAppPGiOS/ManageDocumentLifeCycle/ManageDocumentLifeCycle.html

因此,如果您的用户选择iCloud,那么您应该使用iCloud。

iCloud文档模型的持续时间是任何人猜测的,但这就是它今天的工作方式。整个事情似乎是糟糕的UI设计的杰作,因为这个问题的直接答案显示:

 -(NSURL*)ubiquitousContainerURL {

     return [[NSFileManager defaultManager] URLForUbiquityContainerIdentifier:nil];

 } 
相关问题