只需在 ubiquity 容器中创建文件,就可以将文件同步到 iCloud,如下所示:
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *containerURL = [fileManager URLForUbiquityContainerIdentifier:nil];
NSURL *dirURL = [containerURL URLByAppendingPathComponent:@"files" isDirectory:YES];
NSError *error;
[fileManager createDirectoryAtURL:dirURL withIntermediateDirectories:NO attributes:nil error:&error];
NSURL *icloudURL = [dirURL URLByAppendingPathComponent:@"somefile.txt"];
[fileManager createFileAtPath:[icloudURL path] contents:someData attributes:nil];
这很有效,似乎是将文件放入 iCloud 的一种简单直接的方法,但我在文档中找不到此类示例或提示允许使用此类 API。提出问题:
这安全吗? Apple API文档在某处允许这样做吗?它有任何已知的问题吗?
评论:
最佳答案
有一件事我确信使用 iCloud 并不简单!
有太多的异常(exception),我认为它们从未发生过,但它们仍然发生。 (例如:有时 iCloud 向我的应用程序发送了一个 NSConcreteData
对象,但我的应用程序使用了 NSFileWrapper
;有时我的文件变成了一个文件夹,几分钟后它又返回了一个文件)
有很多方法可以访问 iCloud。这是一个简单的方法:
http://www.raywenderlich.com/12779/icloud-and-uidocument-beyond-the-basics-part-1
记住在使用 iCloud 时要小心(任何事情都有可能发生)
祝你好运
关于ios - 使用 NSFileManager API 操作 iCloud 中的文件是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16489382/