I wanted to know how can I save a user's input when the user enters something from his mobile phone in the UITextField?
If I just use the text field and run the app I can enter data in the text field but when I close the application the data is gone. So how can I store that data permanently and show it again after the application is closed and reopened. Is there any way to save it?
At first, you should save the text when user did editing before user close the application(e.g. saved by NSUserDefaults):
self.yourTextView.delegate = self;
- (void)textViewDidChange:(UITextView *)textView
{
if (textView.markedTextRange == nil)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:textView.text forKey:#"userText"];
[defaults synchronize];
}
}
Then, load the text that user saved before when user open your application again:
- (void)viewDidLoad
{
[super viewDidLoad];
self.yourTextView.text = [[NSUserDefaults standardUserDefaults]objectForKey:#"userText"];
}
We can save data into 3 data base
If you want to store single data into db, you can use
NSUserDefault
For store
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:textView.text forKey:#"textviewdata"];
[defaults synchronize];
For Retrieve
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *strTextViewText = [NSString stringWithFormat:#"%#",[defaults objectForKey:#"textviewdata"]];
Then Store a larger amount of data,we can use
SQLite
CoreData
Here are some ways to save data inside application.
create local database using sqlite or coredata both provides facilty to save data locally and before use please find the different situations to use these databases.
using NSUserDefaluts but not recomemded because NSUserDefaults aren’t meant to store sensitive information for more information see imp link see example also if you still.
To store data using NSUserDefaluts:
[[NSUserDefaults standardUserDefaults]setObject:self.textfield.text forKey:#"yourKey"];
To get data anywhere inside app:
object = [[NSUserDefaults standardUserDefaults] valueForKey:#"yourKey"];
Try with this:
-(void)viewDidDisappear:(BOOL)animated
{
[[NSUserDefaults standardUserDefaults]setObject:self.urtextfield.text forKey:#"savedtext"];
}
-(void)viewWillAppear:(BOOL)animated
{
self.urtextfield.text = [[NSUserDefaults standardUserDefaults]objectForKey:#"savedtext"];
}
Related
i have tried to look for a way to manage the number of views and likes on videos in iOS but i don't know the best way to do it or the safest way since i'm saving it to memory do i use NSUserDefault
Yes. You can use NSUserDefault. It is to store the data and you can retreive it from any place of the projects. It can be done by following way:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:yourdata forKey:#"keyName"];
[userDefaults synchronize];
Write the below code where you want to get the saved data in userdefaults.
NSUserDefaults *userDefauls = [NSUserDefau;ts standardUserDefaults];
value = [userDefaults objectForKey:#"keyName"];
Is it possible to save and load data on Today Extension using NSUserDefaults?
After closing the Notification Center, the widget behaves like an app which is terminated, so any data results lost. How could I solve this issue?
This is my code:
NSUserDefaults *defaults;
- (void)viewDidLoad {
[super viewDidLoad];
defaults = [NSUserDefaults standardUserDefaults];
NSArray *loadStrings = [defaults stringArrayForKey:#"savedStrings"];
if ([loadStrings objectAtIndex:0] != nil) {
[display setText:[NSString stringWithFormat:#"%#", [loadStrings objectAtIndex:0]]];
}
if ([loadStrings objectAtIndex:1] != nil) {
calculatorMemory = [NSString stringWithFormat:#"%#", [loadStrings objectAtIndex:1]].doubleValue;
}
}
- (IBAction)saveData:(id)sender {
NSString *displayString;
NSString *memoryString;
NSArray *saveStrings = [[NSArray alloc] initWithObjects: displayString, memoryString, nil];
defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:saveStrings forKey:#"savedStrings"];
[defaults synchronize];
}
You need to use app group identifier instead of com.*
For instance:
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.company.appgroup"];
Don't forget to synchronise when you store data
[shared synchronize];
You need to add the App Group stuff detailed under here and then if it actually worked (pretty iffy under beta) it should allow you to share NSUserDefault data like normal between the host and widget.
Edit: Normal NSUserDefaults does not work. Apple has implemented a new method. To use, simply redefine your NSUserDefaults instance like this:
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"com.you.app.container"];
For anyone wondering how in the world do you save and get values then look at this code.
In your regular app add this to save whatever you like in your *.m file.
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.yourcompanyname.TodayExtensionSharingDefaults"];
//save dic
[shared setObject:dictionary2 forKey:#"dicForTodayWidget"];
//save array
[shared setObject:tempArray2 forKey:#"arrayForTodayWidget"];
//save some value
[shared setObject:#"1234" forKey:#"myValForTodayWidget"];
[shared synchronize];
In your today widget under TodayViewController.m in viewDidLoad add this.
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.yourcompanyname.TodayExtensionSharingDefaults"];
//get dic
NSMutableDictionary *dictionary = [shared objectForKey:#"dicForTodayWidget"];
You first need the App Groups set up for both targets (application and the extension).
Then, use the
NSUserDefaults *shared = [[NSUserDefaults alloc]initWithSuiteName:#"group.company.myapp"];
to obtain the defaults object which you can read from/write to as usual.
If you want to be notified of changes to the defaults, use the NSUserDefaultsDidChangeNotification in your widget (or app).
For a step-by-step tutorial explaining all this, take a look at this blog post.
#edukulele
Today Extension and Main app run on two processes. Today Extension can't receive NSUserDefaultsDidChangeNotifications. I tried use MMWormhole. It is very good.
I'm creating a simple double value, saving it as NSUserDefault and trying to recover it...but it doesn't.
- (IBAction)try:(id)sender {
double value = 42.00;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setDouble:value forKey:#"kDoubleKey"];
// NSLog(#"loading %f",myDouble);
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSUserDefaults *fetchDefaults = [NSUserDefaults standardUserDefaults];
double intValue = [fetchDefaults doubleForKey:#"kDoubleKey"];
NSLog(#"douvle retrieve %f",intValue);
}
Do not forget to synchronise whenever you save something to the defaults:
put this at the end of your try method
[[NSUserDefaults standardUserDefaults]synchronize];
Here is what apple says about this: Because this method is automatically invoked at periodic intervals, use this method only if you cannot wait for the automatic synchronization (for example, if your application is about to exit) or if you want to update the user defaults to what is on disk even though you have not made any changes.
This question already has answers here:
Best way to save data on the iPhone
(7 answers)
Closed 9 years ago.
I want to save some initial settings values given by the user when the app is open for the first time, If the values are saved it shouldn't be appear next time. How to save these values inside the app. Some suggested to use .plist , while searched regarding this. Is that the right approach? or there any simpler option available?
I would suggest saving the information in an array and then saving the array on the NSUserDefaults singleton that is integrated on the device. That way you can always access the information from anywhere.
Have in mind that this approach is only viable if the info is small enough.
To save on the NSUserDefaults class:
[[NSUserDefaults standardUserDefaults] setObject:yourMutableArray forKey:#"Key"];
To get the value:
NSMutableArray *array = [NSMutableArray arrayWithArray:[[NSUserDefaults standardUserDefaults]
objectForKey:#"Key"]];
The easiest option is to save these value in form of key-value pair into NSUserDefaults.
NSUserDefaults *stdDefaults = [NSUserDefaults standardUserDefaults];
if([stdDefaults objectForKey:#"APP_OPENED"] == FALSE)
{
[stdDefaults setValue:#"YOUR_VALUE" forKey:#"YOUR_KEY"];
//Store more values if you wish
[stdDefaults setBool:YES forKey:#"APP_OPENED"];
[stdDefaults synchronize];
}
If you want to save non encrypted data, you can use NSUserDefaults:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:txtfield1.text forKey:#"info1"];
[defaults setObject:txtfield2.text forKey:#"info2"];
[defaults synchronize];
If it includes passwords, better to use KeyChain. Otherwise, NSUserDefaults would be a good choice...
I would suggest using NSUserDefaults. Something like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL hasOpenedAppBefore = [defaults boolForKey:#"hasOpenedAppBefore"];
[defaults setBool:YES forKey:#"hasOpenedAppBefore"];
You can use NSUserDefaults which can store data
Read apple's doc
Make use of NSUserDefaults to store the data. The stored data can retrieved and modified whenever necessary.
Read NSUserDefaults Class Reference
Read Tutorial
NSUserDefaults is the best option for store data inside application and you can easily use it throughout the application.
I want to display a help message on a view controller when the app is installed and opened for the very first time ONLY.
Is there a method I can use to do this?
You can display the help message once, and then store a boolean value in NSUserDefaults to indicate that it should not be shown again:
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
BOOL appHasBeenLaunchedBefore = [userDefaults boolForKey:#"HasBeenLaunched"];
if (!appHasBeenLaunchedBefore)
{
[self showHelpMessage];
}
[userDefaults setBool:YES forKey:"HasBeenLaunched"];
Use Grand Central Dispatch's dispatch_once() and check some persistent storage.
static dispatch_once_t pred;
dispatch_once(&pred,^{
NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
BOOL hasLaunched = [userDefaults boolForKey:kAppHasLaunched];
if (!hasLaunched) {
[self showFirstLaunchMessage];
[userDefaults setBool:YES forKey:kAppHasLaunched];
}
});
This is the easiest way to ensure code only gets run once in your app per launch (in this case the block will only be executed once). More information about this is in the Grand Central Dispatch Reference. In this block you simply need to check some persistent storage, such as your preferences, to see if your app has ever launched before and display a message as appropriate.
Use a key in the user defaults. For example:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
BOOL launchedBefore = [userDefaults boolForKey:#"hasRunBefore"];
if(!hasRunBefore)
{
NSLog(#"this is my first run");
[userDefaults setBool:YES forKey:#"hasRunBefore"];
}
The user defaults are backed up by iTunes, so it'll normally be the user's first launch rather than the first launch per device.
EDIT: this is explicitly the same answer as e.James gave before me. The 'other answers have been posted' bar failed to appear. I'll leave it up for the example code but don't deserve credit fir a first answer.