【发布时间】:2019-12-09 03:41:55
【问题描述】:
这可能是我的解决方案
我怀疑我的问题是因为我没有打开“iCloud”功能,但因为我有一个免费的开发者帐户,所以我不能这样做。
如果打开“iCloud”功能是解决方案,是否还有一些说明这一点的文档?
我只找到了documentation regarding "CloudKit",它从不指代“iCloud Drive”。
在这个website 上有一些指向其他文档的链接。
问题陈述
iCloud Drive 中的文件夹结构:
- “TestApp”(iCloud Drive 中应用名称的目录)
- “测试”(目录)
- “testFile 1.txt”(使用
UIDocumentBrowserViewController打开文档) - “testFile 2.txt”(尝试以编程方式打开文档)
- “testFile 1.txt”(使用
- “测试”(目录)
如果我使用UIDocumentBrowserViewController (documentation) 打开目录中的文档,我可以毫无问题地调用document.open(...)(document 是UIDocument 的子类)。但是,如果我想以编程方式访问文件夹中的其他文件,则会出现错误:
Error Domain=NSCocoaErrorDomain Code=257 "无法打开文件“testFile 2.txt”,因为您没有查看权限。" UserInfo={NSFilePath=/private/var/mobile/Library/Mobile Documents/com~apple~CloudDocs/TestApp/test/testFile 2.txt, NSUnderlyingError=0x2829d20a0 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not allowed"}}
我如何尝试以编程方式访问“testFile 2.txt”
当用户打开“testFile 1.txt”时,我得到它的 url,即:
"file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/TestApp/test/testFile%201.txt"
现在我正在使用以下代码尝试访问“testFile 2.txt”(另请参见内联 cmets):
// I get this url from the delegate method `UIDocumentBrowserViewControllerDelegate.documentBrowser(_:didPickDocumentsAt:)`
let file1URL = // ...
let file2URL = file1URL
.deletingLastPathComponent()
.appendingPathComponent("testFile 2")
.appendingPathExtension("txt")
let success = file2URL.startAccessingSecurityScopedResource() // returns `false`
TestDocument(fileURL: file2URL).open{ success in
print(success) // prints `false` and see ERROR above
}
// checking existence
let fm = FileManager.default
fm.isUbiquitousItem(at: file1URL) // returns `true`
fm.fileExists(atPath: file1URL.path) // returns `true`
fm.isUbiquitousItem(at: file2URL) // returns `false`
fm.fileExists(atPath: file2URL.path) // returns `false`
如您所见,文件管理器的“testFile 2.txt”“不存在”。
【问题讨论】:
标签: ios icloud icloud-drive