0

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

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

我想访问这个文件夹,但是:

~/Library/Mobile Documents

我怎样才能轻松地将其作为路径值访问?我可以用类似的方式做到这一点吗?

4

3 回答 3

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];

 } 
于 2012-11-29T22:34:31.177 回答
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,因此硬编码方法可能是您唯一的选择。

于 2012-11-29T22:22:11.257 回答
0

在我最近的 macOS 应用程序中,我有同样的需求:如何访问 iCloud 目录的根文件夹。

重要的! 这是为非沙盒版本编写的,因为该应用程序仅适用于我自己。如果您计划在 Mac App Store 上发布应用程序,请不要关闭沙盒版本。

  1. 我在应用程序的权利文件中关闭了沙盒。
  2. 此代码将访问您的 iCloud 根文件夹:

    let pathToiCloudFolder = NSString(string: "com~apple~CloudDocs").expandingTildeInPath
    
    let backUpFolderUrl = FileManager.default.urls(for: .libraryDirectory, in:.userDomainMask).first!
    let backupUrl = backUpFolderUrl.appendingPathComponent("Mobile Documents/" + pathToiCloudFolder)
    
    print("Backup Folder:", backupUrl)
    
于 2020-05-08T13:38:55.287 回答