How to save the state of the stepper in iOS? - ios

I’m doing an app to be used as an inventory.
I need to know how to save the state of the stepper
So if I or another user close the app, the state of the stepper won’t change.
My app is targeting multiple users, so if one user has changed the stepper state it will be saved and changed for other users.

The main idea: save UIStepper's value.
It depends on your needs how you should save it:
If you have just one main stepper and this is the whole idea of your
app, you can save value to UserDefaults and then retrieve it
let stepper = UIStepper()
// Saving
UserDefaults.standard.set(stepper.value, forKey: "stepperValue")
// Retrieving
stepper.value = UserDefaults.standard.double(forKey: "stepperValue")
If you have various number of steppers and their values are also connected with some model, you should use FileManager to save encoded values of steppers or you can also use some database like CoreData or Realm.

Related

Delete saved data form UserDefaults at a specific time?

I'm using UserDefaults with SwiftUI to save my data from multiple variables and I use them for graphs which shows some daily information. Is there a way to delete or reset the saved data form UserDefaults to only contain the data form the current day?
For example delete the data at 23:59 every night.
#Published var waterGraph: Float = UserDefaults.standard.float(forKey: "waterGraph") {
didSet {
UserDefaults.standard.set(self.waterGraph, forKey: "waterGraph")
}
}
Do you really need to delete the data from your UserDefaults at exactly 23:59? If you do then you would have to run background tasks which can be quite complicated. Another easier alternative is to clear the UserDefaults each time the user opens the app and if it has been 24 hours since your UserDefaults data was stored. Store a value in your UserDefaults called "in24hours" and set its value to 24 hours from the current time. In the appDelegate's didFinishLaunchingWithOptions function, check for the date stored in the userDefaults "in24hours" and if the current time is greater than the time in that variable, then you can clear your userDefaults there. You can set the "in24hours" variable whenever you want (after you add your to be deleted data in UserDefaults). Instead of setting the "in24Hours" to 24 hours from the current time and date, you could also set it to midnight on the following date.

Showing random data from core-data using Swift 3

I need some help getting random data from core data using Swift 3, Xcode 8.3.1. I currently have an app that creates a list in tableview using data that is entered by the user.. (user enters a name and takes a picture of that person) The entity "Friend" holds the attributes "name", "image".
The first version of this app was just a name and I would use arc4random to randomly update a label with a name on a modally presented VC on a button click. The names were simply stored in an Array.
This version is including an image so I decided to try my hand at core-data (never used it before) and now I'm stuck at my random select button. Currently the app will store the data fine and then retrieve it and display everyone alphabetically along with their image in a tableview. As a new person is submitted the info gets stored and the tableview updates.
I need to show a randomly selected name and its image, but I don't know how to do this and research has failed me on getting it done.
If there is a better way of storing an image & name instead of core-data I'm open to changing as well. The app stores anywhere from 20-80 different names. It will never be used to store much more than that.
You can fetch your items from the context, which will give you an array of objects. Now you just use your favorite random function to get a random index for this array. And then use an object at that index.

Swift 3 How to save and load data

I'm a software development student and for a school project me and a friend are making a iOS Swift game and we are learning Swift 3 as we progress in the game aspects.
Now, we have come to the part of the development where we question how is game data going to be saved (I'm talking about scores, player's name, in-game money, if the player plays in mute or unmute.. you know that kind of stuff)
The game uses an avatar and the player can customize it, so data customization needs to be saved too.
Right now we manage this data throught variables like
var eyeColor = "#04ff45"
var eyetype = 3 // And so on
I have done some research on this and found that there are various methods we could use.
Like, we could use Core Data or a Dictionary or a JSON file.
I'd like to know which could be the best/easiest way to acomplish this, if we need to use any specific libraries and some basic code on how to write/read the data
Thank you very much
Best way to save scores is by using NSUserDefaults
To save settings such as volume, you can follow the model I have below.
//When you tap on the mute button, execute this code.
if soundSwitch { //true
UserDefaults.standard.set(false, forKey: "SoundSwitch")
soundSwitch = false
} else { //false
UserDefaults.standard.set(true, forKey: "SoundSwitch")
soundSwitch = true
}
If you are saving players money, name and all this other stuff, it would be better to save them in some kind of database. I'm not sure if you can do that with GameCenter.
The easiest way to save data you are working with on the device would be to use NSUserDefaults. All you have to do is create a key, and store a value under that key. You can then use the key to access the value later and it is stored on the phone's permanent memory.
For example, you would use the user defaults to create a key called "eye color", store a value under that key, and then access it later using the term that you know the value is stored under (i.e. the key). This is a very simple way of storing such information.

How to save entered data to make it available after app restart

I have my own custom data structures which receive input through the ViewController class. The interface is a simple text field, which is linked to a variable that the contents of the text field is copied to. Upon launching the app, the text field should be prepopulated with data entered in the past. However, as soon as I close the app, the data is lost. I am new to programming and assume this can be remedied by implementing the necessary functions in AppDelegate class, more specifically, under the default applicationWillTerminate function. If this is correct, how do I implement the data saving process? If not, where & how do I make sure data entered is stored so that fields are prepopulated the next time the app is opened?
If you need to store small amount of data, take a look at NSUserDefaults.
If your data better fits to a database, you can use SQLite (may be with a wrapper) or Core Data.
There is also a modern but not yet very mature cross-platform mobile database called Realm (partially open-sourced at the moment).
Since you are saving TextField data(which use mostly small string text) Use NSUserDefaults to store the string for persistence. What you need to do is at textFieldEndEditing save the text to NSUserDefaults and in viewDidLoad assign it to textField.
Saving To NSUserDefaults:
let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(myTextField.text, forKey: "TextFieldText")
defaults.synchronize()
Retrieving from NSUserDefaults:
let defaults = NSUserDefaults.standardUserDefaults()
if let savedText = defaults.stringForKey("TextFieldText")
{
print("Textfield Text: \(savedText)")
}

How to store form data in a swift app

I'm working on my first app and I need some help understanding what kind of data storage I need. I'm familiar with PHP, SQL, and databases but I don't know where to start with this project.
The app is pretty much a basic form with text fields, pickers, and uploaded images. At the end, the user will press submit and all of their data will be sent in an email.
What is the best way to store their data, so the user can go to previous screens and have their previously entered info still there. And then what is the best way to store the data after they press submit to send it in an email?,
Thanks so much for your help!
If it's just form data that you're storing once for submission, for simplicity sake, I recommend just stuffing it in a global dictionary that you can access from different views. Swift makes this easy by just adding an empty swift file and defining your dictionary at the top:
var myFormData: [String: AnyObject]()
You can now access "myFormData" form anywhere in your app, add and remove stuff from it.
You shouldn't technically need to "reload previous views" because of the way the navigation stack works. Anything you go back to should hold it's info.
If you really need to save the data to allow the user to close the app and then pick up where they left off much later, then I recommend simply kicking your dictionary to NSUserDefaults. It doesn't sound like something that needs to involve a database.
You can use a class called NSUserDefaults
var defaults: NSUserDefaults = NSUserDefaults.standardUserDefaults() //make an instance
defaults.setObject("Bob", forKey: "myName") //storing a value
defaults.synchronize() //synchronize data
println(defaults.objectForKey("myName")!) //retrieve the data

Resources