UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Storing data in the user's file system

Forums > SwiftUI

Inspired by Obsidian, I am trying to build a iOS and MacOS journaling app that stores entries as Markdown files. The idea being that the Markdown format is future proof and if entries are stored in the user's file system, they will own their data. So the app I wanted to build would need to create, read, update, and delete markdown files in a directory provided by the user.

User interface example: journal wireframes

File structure example:

/directory-provided-by-user
--> 202203050000.md
--> 202203040000.md
--> 202203030000.md
--> 202203020000.md

I also wanted there to be an iOS and MacOS app so that entries could be created on the fly in mobile, but also on one's computer. This would mean the files would have to sync across devices and I thought iCloud would be a good option for that.

The problem I am running into is accessing files in iCloud Drive. iOS apps are sandboxed and I haven't found a good example of how to access a directory of files outside that sandbox. I have found a couple of options for MacOS but nothing that's easy to grasp. Also I have looked for Atom of Sublime clones in Swift, with no luck.

I liked the idea of document-based apps in SwiftUI, but this app is meant to work with a collection of entries, not just one entry at a time. From what I can tell, document-based apps are good for application like Pages, where each document can stand on its own. The journaling app is meant to be a collection of entries and offer features like sort, search, review random entry, etc.

Questions I could use help with are:

  • How can an app be granted access to user, not sandbox, iCloud files? And once granted how does an app perform CRUD actions on those files?
  • If that is not posible, what options are there for writing to user accessible files, but also syncing them across mobile and desktop?

2      

I don't have an answer for your syncing issues. But you can use a document-based app, allowing people to have multiple journals. Each journal would be its own document, with the document holding the individual Markdown files. Use file wrappers (look at the NSFileWrapper class) to save the individual Markdown files in the document. The following GitHub project shows how to save a collection of Markdown files in a single document in SwiftUI:

WikiDemo

2      

@SwiftDevJournal thank you. This is very close to what I need, but not quite. From one the articles linked in the wiki you provided, Using File Wrappers in a SwiftUI App:

By using a file wrapper you can save all these files and have it look like a single file to the person using the app.

I actually want users to see and have access to the markdown files created, similar to the Obsidian app. However, part of what you said might be able to help me. I could have journal files that are recognized by the app (e.g. myjournal.jrnl) and the app could work like Xcode, where the app recognizes and add configuration to that file, but could also create, read, update, and delete files in the directory file that the .jrnl files lives in. The main difference between that and what your recommendation being that those other files wouldn't be bundled into the original .jrnl file.

Update to example file structure:

/directory-copies-journal-name
--> journal-name.jrnl
--> 202203050000.md
--> 202203040000.md
--> 202203030000.md
--> 202203020000.md

2      

I guess the next thing I would have to figure out is how to excape the app sandbox. I am assuming it's possible since that is how Xcode functions, but I'm just not super familiar with FileManager.

2      

If you don't want the file wrapper to appear as a single file, make sure the document type doesn't conform to com.apple.package. If the document doesn't conform to com.apple.package, the file wrapper will appear as a folder in the Finder (Mac) or Files app (iOS). On Mac you'll be able to access the Markdown files inside the folder. I'm not sure if the Files app on iOS has a way to access the files inside a folder in a file wrapper.

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.