UIDocument不保存到文件,尽pipe指示成功
我试图用UIDocument
打开,修改和保存iCloud Drive中的文件。 当我使用文件位置调用save(to:for:completionHandler:)
并对.forOverwriting
使用UIDocumentSaveOperation
,它的状态为success = true
。 但是,iCloud文件(如桌面和iOS文件浏览器中所示)不会更新,并且在重新打开文件时,不会显示更改。 我已经validationcontents(forType:)
在保存时返回正确(修改)的文件内容。
(注意:我已经看过这个问题 ,但这不是很有帮助)
以下是代码的相关部分:
MainViewController.swift:
var saveFile: SBDocument? @IBAction func bbiOpen_pressed(_ sender: UIBarButtonItem) { if saveFile == nil { let importMenu = UIDocumentMenuViewController(documentTypes: self.UTIs, in: .import) importMenu.delegate = self importMenu.popoverPresentationController?.barButtonItem = bbiOpen self.present(importMenu, animated: true, completion: nil) } else { willClose() } } func willClose(_ action: UIAlertAction?) { if saveFile!.hasUnsavedChanges { dlgYesNoCancel(self, title: "Save Changes?", message: "Would you like to save the changes to your document before closing?", onYes: doSaveAndClose, onNo: doClose, onCancel: nil) } else { doSaveAndClose(action) } } func doSaveAndClose(_ action: UIAlertAction?) { saveFile?.save(to: saveFileURL!, for: .forOverwriting, completionHandler: { Void in self.saveFile?.close(completionHandler: self.didClose) }) } func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) { saveFile = SBDocument(fileURL: url) saveFile!.open(completionHandler: { success in self.finishOpen(didCompleteSuccessfully: success) }) } func finishOpen(didCompleteSuccessfully result: Bool) { if result { print(saveFile!.localizedName) saveFileURL = saveFile!.fileURL saveFileName = saveFile!.localizedName self.navTitleBar.prompt = saveFileName bbiOpen.title = NSLocalizedString("titleClose", comment: "Close") bbiOpen.style = .plain } else { saveFile = nil } } @IBAction func bbiSave_pressed(_ sender: UIBarButtonItem) { self.saveFile!.save(to: self.saveFileURL!, for: .forOverwriting, completionHandler: self.didSave) } func didSave(_ success: Bool) { guard success else { print("Error saving soundboard file to \(String(describing: saveFileURL))") return } print("File saved successfully") }
SBDocument.swift:
class SBDocument: UIDocument { override var fileType: String? { get { return "com.whitehatenterprises.SoundBoardFX.sbd" } } override var savingFileType: String? { get { return "com.whitehatenterprises.SoundBoardFX.sbd" } } override init(fileURL url: URL) { super.init(fileURL: url) } override func contents(forType typeName: String) throws -> Any { let arr = NSArray(array: SoundEffects) let data: NSData = NSKeyedArchiver.archivedData(withRootObject: arr) as NSData return data } }
更新:我真的需要帮助,我已经尝试了所有我能想到的方法来解决这个问题。 任何援助,你可以给我将不胜感激。
所以根据
https://developer.apple.com/reference/uikit/uidocument
它看起来像保存function实际上不是保存文档。 我从阅读的理解是,保存只是为了创build一个新的文件。 我知道你正在使用.forOverwriting来保存它,但是在iCloud中可能会有一些不会让整个覆盖发生的东西。
在您的doSaveAndClose方法尝试调用
self.saveFile?.close(completionHandler: self.didClose)
通过它自己。 如果查询文件是否存在,则可能需要执行一些types的查询。 如果没有,则调用.save(),否则调用.close函数。 看来,无论什么时候它closures的文件保存更改。
最初的文件生成为我工作的方式是:
let doc = YourUIDocumentClass(fileURL: fileURL) doc.save(to: fileURL, for: .forCreating) { success in ... }
然后修改文件,然后执行:
doc.save(to: fileURL, for: .forOverwriting) { success in ... }
完成后。 随后对文件的访问通过以下方式完成:
doc.open() { success in ... } doc.close() { success in ... }
你可能还需要做一个:
doc.updateChangeCount(.done)
而文件是打开的告诉文件有未保存的变化。 只需设置这将在几秒钟后保存。 你甚至不需要近距离这样做。
意味着你要么嵌套所有这些,要么确保他们之间有足够的时间,以便他们完成。