如何从我的iOS应用程序中访问iCloud Drive中的文件?

有没有办法从iCloud Drive中选择与UIImagePickerController()相似的文件?

您需要启用iCloud权利。 完成此操作后,您可以通过以下方式显示控制器:

 let documentPickerController = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypeImage), String(kUTTypeMovie), String(kUTTypeVideo), String(kUTTypePlainText), String(kUTTypeMP3)], inMode: .Import) documentPickerController.delegate = self presentViewController(documentPickerController, animated: true, completion: nil) 

在您的委托中实现方法:

 func documentPicker(controller: UIDocumentPickerViewController, didPickDocumentAtURL url: NSURL) 

Swift 4.X

您需要在XCodefunction中启用iCloud权利。 此外,您必须在Apple的开发者帐户中打开应用程序包中的iCloud 。 完成此操作后,您可以通过以下方式显示文档选择器控制器:

使用UIDocumentPickerDelegate方法

 extension YourViewController : UIDocumentMenuDelegate, UIDocumentPickerDelegate,UINavigationControllerDelegate { func documentMenu(_ documentMenu: UIDocumentMenuViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) { documentPicker.delegate = self self.present(documentPicker, animated: true, completion: nil) } func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { print("url = \(url)") } func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) { dismiss(animated: true, completion: nil) } } 

为Button Action添加以下代码

 @IBAction func didPressAttachment(_ sender: UIButton) { let importMenu = UIDocumentMenuViewController(documentTypes: [String(kUTTypePDF)], in: .import) importMenu.delegate = self importMenu.modalPresentationStyle = .formSheet if let popoverPresentationController = importMenu.popoverPresentationController { popoverPresentationController.sourceView = sender // popoverPresentationController.sourceRect = sender.bounds } self.present(importMenu, animated: true, completion: nil) } 

这对我来说很好。 希望它也能帮到你。

快乐编码:)

当用户选择应用程序沙箱外的目标时,文档选择器会调用委托的documentPicker:didPickDocumentAtURL:方法。 系统会将文档副本保存到指定目标。 文档选择器提供副本的URL以指示成功; 但是,您的应用无权访问此url引用的文件。 链接

这段代码对我有用:

 func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) { let url = urls[0] let isSecuredURL = url.startAccessingSecurityScopedResource() == true let coordinator = NSFileCoordinator() var error: NSError? = nil coordinator.coordinate(readingItemAt: url, options: [], error: &error) { (url) -> Void in _ = urls.compactMap { (url: URL) -> URL? in // Create file URL to temporary folder var tempURL = URL(fileURLWithPath: NSTemporaryDirectory()) // Apend filename (name+extension) to URL tempURL.appendPathComponent(url.lastPathComponent) do { // If file with same name exists remove it (replace file with new one) if FileManager.default.fileExists(atPath: tempURL.path) { try FileManager.default.removeItem(atPath: tempURL.path) } // Move file from app_id-Inbox to tmp/filename try FileManager.default.moveItem(atPath: url.path, toPath: tempURL.path) YourFunction(tempURL) return tempURL } catch { print(error.localizedDescription) return nil } } } if (isSecuredURL) { url.stopAccessingSecurityScopedResource() } navigationController?.dismiss(animated: true, completion: nil) }