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.
Related
I develop iOS app, which is using Core Data and I would like to create same sample data records when the app is installed. Is there any easy way how to do it..?
For example there is entity "Employee" and I would like to add "John Doe" employee when the app is installed.
Can someone help me with that?
Thanks
Is a pre-populated database for coredata what you want ? If so, I think you should find this link interesting : Any way to pre populate core data?
Otherwise, you can maybe save in user defaults a boolean to know if it is the first time that the application has been launched. If this is the first time, you write some code to add the objects you want.
Thanks Sunder for you answer.
In the end I decided for fast solution by checking whether the app is running first time in the AppDelegate didFinishLaunching method:
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (![userDefaults valueForKey:#"PCAppDelegateFirstRun"] )
{
[PCDatabase createSampleData];
[userDefaults setBool:YES forKey:#"PCAppDelegateFirstRun"];
}
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.
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.
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.
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"];