Force a Save on a Document Based iOS App in SwiftUI

Currently, I am developing a small document based App for iOS with SwiftUI. I was quite surprised to see that there is no obvious way to save the document’s contents to file after editing. In fact, the default behaviour was that nothing was saved at all after editing my document’s content. On macOS there is at least the possibility to update the change count to force a save later but that is not available on iOS SwiftUI.

Well, there is this hint on stackoverflow to register an undo at the UndoManager even if you do not use it. For some Views that worked for me; for others it didn’t. I was getting tired to spend hours in investigating why this was so. So I was looking for a way to save the document’s content explicitly. This is what I came up with.

In my app the document’s content (my model) is encoded as a PropertyList. So why not writing this PropertyList by myself? In MyDocument I create my model and keep a reference on it. In my model I store the fileURL of my document’s file on creating or opening a document:

So I can access this URL in my model:

The only problem with this is that outStream.write is not that simple as it looks here. It needs some kind of buffer pointer which is not that easy to create in Swift 5. But here is this nice extension which does the job for me:

Now I can save the content of my document wherever I want it without using any dummy undo manager.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert