我已成功获取存储在iCloud驱动器中的文件列表。当我检查路径中存在的文件时,它返回true。
但是当我尝试获取文件的内容时,它不允许我读取文件并提供一些垃圾数据。
我可以直接从iCloud读取文件,还是需要先将其移至app的文档文件夹? 在此先感谢您的帮助。
这是我的代码
iCloud网址
static let iCloudDocumentsURL: URL? = FileManager.default.url(forUbiquityContainerIdentifier: nil)?.appendingPathComponent("Documents")
文件路径
let filePath =DocumentsDirectory.iCloudDocumentsURL?.appendingPathComponent(file).path
检查文件是否存在及其显示"文件可用"
if FileManager().fileExists(atPath: filePath)
{
print("File Available")
}
else
{
print("Not Available")
}
获取文件数据并显示 176 字节,实际上是 76756 字节
let fileData = NSData.init(contentsOfFile: filePath)
答案 0 :(得分:-2)
此目标C代码可以帮助您从iCloud中读取文件......
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.content"] inMode:UIDocumentPickerModeImport]; //public.composite-content
documentPicker.delegate = self;
[documentPicker setModalPresentationStyle:UIModalPresentationFormSheet];
[self presentViewController:documentPicker animated:YES completion:nil];
实施UIDocumentPickerDelegate
方法
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
NSLOG(@"controller.documentPickerMode = %d", controller.documentPickerMode);
if (controller.documentPickerMode == UIDocumentPickerModeImport) {
//here you can download your file and store in document dir.
//in "url" you get url of the file to be downloaded
//so you can check here whether file is already available or not
}
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
NSLOG(@"documentPickerWasCancelled");
}