I'm trying to implement a UIDocumentBrowserViewController
(new in iOS 11). I can create documents but I can't save them.
My UIDocument
subclass overrrides the contents(forType typeName: String)
method
override func contents(forType typeName: String) throws -> Any {
guard let data = content.data(using: .ascii) else { return Data() }
return data
}
My file editing ViewController gets instantiated with the document and a button press should save the document.
@IBAction func dismissDocumentViewController() {
document?.content = self.contentTextView.text
let url = self.document?.fileURL
self.document?.save(to: url!, for: .forOverwriting, completionHandler: { (success) in
print("Saved \(success)")
self.dismiss(animated: true, completion: nil)
})
}
Calling save does not save the document and I also tried as suggested in this answer: https://stackoverflow.com/a/43571511/4089350
self.document?.close(completionHandler: { (success) in
print("closed \(success)")
self.dismiss(animated: true, completion: nil)
})
It prints that it closed the document but it isn't actually saved.
How do I save a UIDocument correctly? Thanks in advance.