【问题标题】:Read Content Of File from iCloud从 iCloud 读取文件内容
【发布时间】:2017-08-13 00:13:39
【问题描述】:

我已成功获取存储在 iCloud 驱动器中的文件列表。当我检查文件是否存在于路径中时,它返回 true。

但是当我尝试获取文件的内容时,它不允许我读取文件并提供一些垃圾数据。

我可以直接从 iCloud 读取文件还是需要先将其移动到应用程序的文档文件夹? 提前感谢您的帮助。

这是我的代码

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)

【问题讨论】:

  • 你就是这样读取文件内容的?像文本文件这样的文件?哪个 Swift 版本?对于 Swift 3?
  • 使用 Swif3。读取文件没有问题。如果我从文档目录中读取文件,它的工作正常。
  • @GirishM 你解决了吗?

标签: ios swift3 icloud nsfilemanager


【解决方案1】:

此目标 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");
}

【讨论】:

  • 我不想显示文档选择器。我已经在文档选择器上工作过。在我的项目中,我需要提供从文档文件夹或带有自定义 UI 的 iCloud 中选择文件的选项。
猜你喜欢
  • 1970-01-01
  • 2017-04-19
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 2012-01-29
  • 1970-01-01
  • 2020-07-05
  • 1970-01-01
相关资源
最近更新 更多