1

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.

CodeBender
  • 35,668
  • 12
  • 125
  • 132
Nico S.
  • 3,056
  • 1
  • 30
  • 64
  • Have you called updateChangeCount() so it knows there are changes in need of saving? – uliwitness Aug 28 '17 at 07:50
  • Prove that `self.document` is not `nil`. Conversely, prove that the document is "not saved". — Also I would point out that your way of "saving" a document (in your `contents(forType:)` implementation) is not very robust. Have you tried putting a breakpoint to see what `data` is? – matt Nov 05 '17 at 00:50

0 Answers0