This question already has answers here:
How to save local data in a Swift app?
(12 answers)
Closed 6 years ago.
I have a simple swift app that pulls JSON information from online and uses it in a table view. For example, a document would have a name, description, etc. As well as an url to display the pdf. I know how to open the PDF from both local storage and an online url, but if I had an "available offline" value that could be either true or false, where would I store it?
I cannot put it as a JSON key because then it would change the setting for all users accessing the online JSON files, so where do I put a simple device specific option such as this?
NsUserDefault is the best choice for you.
You have to store single variable so.
Coredata is used to small tiny database like if you want to store your data like name , description than you can use coreData.
Here you simple store with NSUserDefaults
Edited:-
To store:-
let value = NSUserDefaults.standardUserDefaults()
value.setInteger (10,forKey: "value")
// here you can use setBool setDouble etc.
To retrieve:-
let num = value.objectForKey ("value") as? Integer
NSUserDefaults is great for easy tiny-scale storage, like remembering settings.
It works like this:
[[NSUserDefaults standardUserDefaults] setObject:#"object" forKey:#"this is my key"];
Then, later,
[[NSUserDefaults standardUserDefaults] objectForKey:#"this is my key"];
//this gives you your object
You just have to be careful with typing, otherwise your app will crash.
Here's the docs for NSUserDefaults:
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSUserDefaults_Class/
Related
This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 5 years ago.
I know this is a frequently asked question and probably the most debated one on Stackoverflow overall. However I have now been searching for the answer for about one week without any luck.
Here is what I want to do..
I have two ViewControllers lets call them mainVC and settingsVC.
In my settingsVC I have a function where the user can put their hourly salary in a textfield that then saves using UserDefaults. This is the data I want to access in the mainVC.
However they don't have any connection to each other (their not bind by any segue and I don't want them to be) and I don't want the mainVC to be instantiated when I save my data.
Furthermore I do not want to use global variables. So what is the best way to do this?
To conclude I simply want to retrieve the data from my settingsVC in my mainVC without using global variables and segues.
You should Use Instance variables , Protocols or Notification for passing data between viewContoller.
Why wouldn‘t you retreive the data from the user defaults? That‘s where the data is stored and is universally available.
You are saving your data in NSUserDefaults. So you have no chance to worry for saving and retrieving data.
To save value
NSString *valueIWantToSet = #"valueToSave"
[[NSUserDefaults standardUserDefaults] valueIWantToSet forKey:#"saveKey"];
[[NSUserDefaults standardUserDefaults] synchronize];
To get this value
NSString *savedValue = [[NSUserDefaults standardUserDefaults]
stringForKey:#"saveKey"];
In case of not saving values in NSUserDefaults, you have to use Notification
I'm making an app with a list of the products of a company. The data does not need to be modified by the user. How can I save this information? The data must already exist locally when you download the app.
As per your requirement Plist or SQLite is good option for you.
Why Plist
Because it is lightweight.
Why SQLite
If you want to perform query on your data.
You can save the data in sqlite or coredata. For already filling the data in database you can use "sqlite manager" and run your queries in sqlite manager and save it on desktop(where you want). After create the filled database drag it into your project and do whatever your want. You have already filled database here.
You can go with either NSUserdefaults or CoreData, additionally there is a third party library called Realm.
Also check this question:
storing data locally on the iphone
Edit
The answer was provided by Sr.Richie in the link:
For simple data you should use NSUserDefaults. CoreData is very cool but mainly to store DB structures, and introduces complexity (but i love it:)). If you just need to store String, Array and so on (basically prefs), you can go with NSUserDefaults:
For example:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; //load NSUserDefaults
NSArray *fakeFavs = [[ NSArray alloc] initWithObjects:#"2",#"4", #"100", nil]; //declare array to be stored in NSUserDefaults
[prefs setObject:fakeFavs forKey:#"favourites"]; //set the prev Array for key value "favourites"
I am trying to use NSUserDefaults to share my data with the WatchKit files. I am trying to have it so when the button function is executed the watchLabel.setText to data from an array. The array and function that selects the data was created in the iOS application. (Unfortunately many resources I come across are in objective-C or extremely vague.)
I have set up app groups for both iOS and Watchkit
In my iOS Swift file I am trying to share my array(arrayBook) and function(.randomData) to use with the WatchKit controller. PS: I have no compiler errors.
let sharedDefaults = NSUserDefaults(suiteName: "group.applewatchtest")
sharedDefaults?.arrayForKey(arrayBook.randomData())
sharedDefaults?.synchronize()
Watchkit Controller; Unsure the correct syntax for fetching data and setting the label to random string from the array. - This is within the UIButton func.
let sharedDefaults = NSUserDefaults(suiteName: "group.applewatchtest")
let sharedArray = sharedDefaults?.arrayForKey("Shared")
watchLabel.setText(arrayBook.randomData())
Where am I going wrong when fetching the data to set the label to data from the array which is stored in my iOS application.
sharedDefaults?.arrayForKey(arrayBook.randomData())
This line of code is not doing anything. You're reading the array from defaults, using a key of whatever arrayBook.randomData() returns, and doing nothing with it.
I assume you want something like
sharedDefaults?.setObject(arrayBook, forKey:"Shared")
Which will write that array into defaults. You then get it back out in the watch (which looks fine) and get a random value using randomData().
I can't figure out how to make the create an account function work in my application... I have my login system set up like so: http://imgur.com/dKGRnok The first set of strings are the passwords. The second set are the usernames. For someone to create an account that allows them to login, I have to make a string manually through the application. How can I make this function so it gets their input and places it as a string as long as the same username doesn't already exist? http://imgur.com/lVzAV8i
You need to create a local database, the proper way would be to use either sqllite or core data to create your structure, then simply add/remove users.
For an sql tutorial for ios follow this:
http://www.raywenderlich.com/913/sqlite-tutorial-for-ios-making-our-app
Or go to this site and check the section that says "Saving and Loading Data"
http://www.raywenderlich.com/tutorials
I personally prefer core data because its the most complete solution once you get used to setting it up.
If what you are asking is a persistent data, [[NSUserDefaults standardUserDefaults] setValue: forKey:] will be the answer.
Once you save a data with NSUserDefaults, the data will be persistent. You can get the value with [[NSUserDefaults standardUserDefaults] valueForKey:]
Added
I misunderstood your question. In this case you should use local database as Chiques said.
Core Data will be the answer.
http://www.raywenderlich.com/934/core-data-tutorial-for-ios-getting-started
What kind of database do you suggest? I want to store user email, username, password, and a couple other random pieces of information. It doesn't have to be fancy. Just a simple database. Are there any free options?
The user information needs to be stored in the keychain to keep it secure.
Any other information could be stored in any one of:
User defaults NSUserDefaults
File on disk (maybe a plist)
Database Core Data (technically just a file on disk)
Which you choose depends on what the data is, how much there is and what kind of access you need to it.
If your data is small and chosen by the user as some kind of setting then user defaults makes sense and is the lowest cost for you to implement.
To use a database, check out Core Data intro.
Wain is right but I think as you want to store small amount of data for further use, the most efficient ways is to use NSUserDefault.
NSUserDefault stores data in NSDictionary type things.
I think this is the step you have to take:
1- check if data exists. I mean if user selected the number if the last run of your app. So in viewDidLoad method:
NSMutableDictionary *userDefaultDataDictionary = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if (userDefaultDataDictionary) {
// so the dictionary exists, which means user has entered the number in previous app run
// and you can read it from the NSDictionaty:
if(userDefaultDataDictionary[LABLE_KEY]){
//and store it
}
}
2 - you can implement some method like syncronize to store data in NSUserDefault every time something has been changed.
- (void) synchronize
{
NSMutableDictionary *dictionaryForUserDefault = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:ALL_DATA_KEY] mutableCopy];
if(!dictionaryForUserDefault)
dictionaryForUserDefault = [[NSMutableDictionary alloc] init];
dictionaryForUserDefault[LABLE_KEY] = //data you want to store
[[NSUserDefaults standardUserDefaults] setObject:dictionaryForUserDefault forKey:ALL_DATA_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];
}
P.S. and don't forget to #define your keys for your dictionary:
#define LABLE_KEY #"Lables"
#define ALL_DATA_KEY #"AllData"
Store it in a plist. If you're talking about data pertaining to one or a few users, that's probably the easy thing. here is a simple example.
Since you say database, store in Sqlite. There's some provided stuff for it already in xcode.
The entire database is contained in one file, which can be moved around if you need to.
Here is some more information on how to use one in your app.