Saving two user data item Strings that are not lost on shutdown - how?

Hi All

Noobie here, just signed up to the Apple Developer program, and I am very excited to be on this journey of fascinating exploration, especially at my ripe old age! Wish me luck!!!

Anyways, I have the makings of something small... an idea, an equally small market (which is fine by the way), and importantly - an app that I've been developing in my little niche - but I do need support in several functional areas to allow me to add those small touches of finesse to the app. One such area is that of maintaining user data once the app is exited or the device powered off/reset.

In my app I have profile page where users can enter their name and one additional detail item that remain live so long as the app is not closed down, or the device remains turned on. However, if the device is powered off, or the app shut down this data is lost. Once the user has entered their name for example, how do I write this back to somewhere such that when they next use the app this data is restored? How would I go about this - in its simplest form, please?

I don't need masses of data stored, no huge database or SQL tables... just two data items that the user can already enter in to a profile page. Simply two String data items.

All advice greatly appreciated.

Many thanks all
John
Hi John,

Welcome to the community!

In your case, User Defaults are the best option, (considering that you don't want to persist user sensitive information, like passwords) and it's the easiest way to start persisting data.
You typically use it to save user preferences-options.

Save Data:
Code Block swift
UserDefaults.standard.set(true, forKey: "loveSwift")


Fetch Data:
Code Block swift
let data = UserDefaults.standard.bool(forKey: "loveSwift")


Other ways to persist data with swift are:


Storing files:

Storing data on the user's file system.

Keychain:

The place where iOS saves all kinds of sensitive user data like usernames, passwords ect.

Core Data:

A framework to manage and persist objects.
It is considered a difficult framework, but from my experience once you learn it you will love it.


If you want to learn more about the above, as it is more complex, you may need to read lots of articles and some books, but the good thing is that Swift has a great community that will help you every step of the way!

Stay safe

Nikos

Saving two user data item Strings that are not lost on shutdown - how?
 
 
Q