Where to put NSUserDefaults to Store Annotations - ios

I came cross this code as shown below. how could I save existing annotation pins info to NSUserdefault without creating any buttons(IBAction)? Should I put NSUserDefault code into viewWillDisappear? Is that the right way to do it?
To save:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setDouble:location.latitude forKey:#"savedCoordinate-latitude"];
[ud setDouble:location.longitude forKey:#"savedCoordinate-longitude"];
[ud setBool:YES forKey:#"savedCoordinate-exists"];
[ud synchronize];

viewWillDisappear is one moment that is often used to save state, but it is not the only place or the only possible place. What if the user suspends your app? You won't get viewWillDisappear. What if viewWillDisappear is not a place where you have access to the annotation information? Perhaps it would better to keep saving info to user defaults as the annotations are created. It depends on the nature and purpose and architecture of your app; it's a problem for you to solve. Your job is to know when your code will run under the event-driven framework, and behave appropriately.

You can put that code wherever you want, when you want to save that data. NSUserDefaults is accessible anywhere. Synchronize is what saves it to disk.

Related

Carrying NSUserdefaults information from one viewcontroller to another view controller

I'm currently working on a simple app, one to really test my skills and understanding of xcode and app development. I'm still a beginner so it's primarily a learning tool as well as skill building. Either way, I'm trying to implement a simple login/register before going on to the next view controller screen with the login and register screen being separate and information stored in the app, not to pharse or icloud.
In ViewController1.m, i call for ViewController2.h:
#import "ViewController2.h"
This is also done in ViewController2.m for ViewController1.h.
In ViewController1.m i utilize NSUserDefaults as such:
NSUserDefaults *defaults = [NSUserDefaults standarUserDefaults];
[defaults setObject:_usernameField2.text forKey:#"username"];
[defaults setObject:_passwordField2.text forKey:#"password"];
[defaults setBool:YES forKey#"registered"];
[defaults synchronize];
When running the program and i am able to register the user and through segue command i'm able to go to the next view controller:
[self performSegueWithIdentifier:#"loginScreen" sender:self];
Ideally when I access the ViewController2 screen I want to use the information from the previous viewcontroller (ViewController1). However, when i attempt to use the information stored the program doesn't acknowledge the information from the previous viewController.
-(void) checkCredintials
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![_usernameField1.text isEqualToString:[defaults objectForKey:#"username"]] && ![_passwordField1.text isEqualToString:[defaults objectForKey:#"password"]])
I've tried other methods such as using NSString however I believe i'm either missing something simple or how i'm attempting to implement the code may be off. Part of me thinks it might be due to the fact that I am running and coding on a later version of xcode (4.6.3) but that's a minuet thought. I've done research and i've tried a few ways, just not sure what i'm missing or what i might need to add or change up. Any help would be much appreciated. Thank you in advance
Set a breakpoint or NSLog the values of the textfields and what is stored for the keys in defaults in checkCredintails method. Also just check that you're actually saving values for _usernameField2 and _passwordField2.
The way you save and retrieve data from NSUserDefaults looks fine, so the user enters(saves) they username and password in ViewController1 and enter it again in ViewController2 to get it verified? You probably shouldn't use NSUserDefaults to check/store passwords, but I guess you're just doing this as an example.

How to fill TableView with data from another ViewController

I want to fill a tableView with some data (Strings), that I save in another View/ViewController.
My viewDidLoad of the SaveViewController:
NSString *savestring = #"Test: This is a test!";
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestring forKey:#"savedstring"];
[defaults synchronize];
Is this a good way to save data? (It's very easy so I am using it at the moment)
Now I am having another View (TableView), that should fill with this string dynamically. (I want to add the date and some string, it should be something like a training journal)
2 . How can I do this? Should I change my "saving" ?
No NSUserDefaults is not a good place to save data with the exception of storing a few user preferences. You should look into using Core Data for your general storage needs.
You can also save to plists which depending on your data can be simpler e.g. you just need to store a single dictionary.
You can sore this changes in some model object. Than you can send this object using delegate, or u can create singleton app manager with general model object. If you want to save this data to database, you should read about CoreData.

Pre populate core data with only one Managed Object

I need to pre populate my core data base with only one Managed Object.
Currently i'm checking on AppDelegate if it's the fisrt time that the app runs, and then a add the object, like this:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL firstTime = [defaults boolForKey:#"firstTime"];
if (firstTime) {
[dataManager insertManagedObject:myManagedObect];
[defaults setBool:NO forKey:#"firstTime"];
[defaults synchronize];
}
insertManagedObject method checks if the managedObject is already in the data base.
It's working fine, but i'm afraid in future app updates this could cause me some kind of trouble, mainly if i change my data model and add a new data model version.
What's the best approach to do that?
Why don't you execute a fetch request a see if the store already
contains that managed object? e.g setting a specific identifier for
that managed object...
Following my comment you could just set up a fetch request against your entity and see if the store already has an instance for it.
This is simple enough to achieve.
If you need to query against a specific object, you could set a property identifier (i.e. a guid) for your entity and use a predicate to see if the object with the specific guid exists or not.
If you share some other details I can give you other suggestions...

Is it possible to not back up some of the files saved in nsdefault?

I used NSDefault in my app to backup some images and got rejected because it uses 6mb of storage.
Can anyone help me add the donotbackup attribute into it? I would like to keep userdefault directory if possible so old users don't lose their images. Any help would be really appreciated :)
My current code is:
to save:
- (IBAction)d1p:(id)sender {
lbl1.text=txt1.text; [txt1 setAlpha:0];
NSString *savestringln1 = lbl1.text;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:savestringln1 forKey:#"savedstringlbl1"];[defaults synchronize];
[self.view endEditing:YES];
}
To load:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *loadstringlbl1 = [defaults objectForKey:#"savedstringlbl1"];
So you want to back up images? NSUserDefaults isn't really meant for that.
Why not try adding iCloud support instead?
The only way to stop something from not being stored in UserDefaults is to just not put it in User Defaults, I am not sure what you mean by backup, you are just duplicating images on the device in two different formats. If the image is on your file system you can store the path to the image in User Defaults if thats useful to you. If you are trying to prevent unintentional deletion can you just mark the 'deleted' images has hidden from the user in some way, a flag, moved to a different location perhaps. You also might want to look into CoreData that can deal with larger amounts of data ad do things like have non serialised properties and such, but that is still just storing your data on the device.

Store two user defined strings in iOS

I am new to iOS programming - I have written an iOS app for a company that uses the app for their workers to log in when they go to work, and to log out when they leave. (They often work on remote places).
All I need now is a way to store two simple strings.
1. String is the user name
2. String is a salted md5 password
Then the user doesn't need to write all the credentials every time he wants to login and out.
What is the simples? SQLite, CoreData, Plst?
Thanks!
See my answer here:
I'd avoid using a plist. The easiest way to save simple data in an application, by far, is NSUserDefaults.
Check out this tutorial for a simple guide on how to use NSUserDefaults. Always be sure to synchronize NSUserDefaults when you're done writing to them.
Example:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// Storing an NSString:
NSString *user = #"MyUsername";
[prefs setObject:user forKey:#"username"];
[prefs synchronize];
Later:
// Retrieving an NSString:
NSString *savedUserName = [prefs stringForKey:#"username"];

Resources