Mat*_*att 24 save ios icloud uidocument swift
我正在尝试使用iCloud Drive打开,修改和保存文件UIDocument.当我打电话save(to:for:completionHandler:)与文件的位置和使用.forOverwriting的UIDocumentSaveOperation,它的状态完成success = true.但是,iCloud文件(如桌面和iOS文件浏览器中所示)不会更新,重新打开文件时,不会显示更改.我已验证contents(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")
}
Run Code Online (Sandbox Code Playgroud)
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
}
}
Run Code Online (Sandbox Code Playgroud)
更新: 我真的需要帮助,我已经尝试了解决这个问题的一切.您可以给我的任何帮助将不胜感激.
小智 0
所以根据
https://developer.apple.com/reference/uikit/uidocument
看起来保存功能实际上并不是用于保存文档。我从阅读中了解到,保存仅用于创建新文档。我知道您正在使用 .forOverwriting 来保存它,但 iCloud 中可能有某些东西不会让完全覆盖发生。
在你的 doSaveAndClose 方法中尝试调用
self.saveFile?.close(completionHandler: self.didClose)
Run Code Online (Sandbox Code Playgroud)
通过它自己。您可能需要执行某种类型的 if 查询来检查文件是否存在。如果没有,则调用 .save(),否则调用 .close 函数。似乎无论文档何时关闭,它都会保存更改。
| 归档时间: |
|
| 查看次数: |
1235 次 |
| 最近记录: |