I am fairly new to Xcode and i need to know how to retrieve the data from a property list make a log in. Would i use this for a log in? Any way to make the code better? I I gave you all the code to look at and hopefully figure out a way.
My .h file
@interface Register : UITableViewController{
IBOutlet UITextField *usernameEntered;
IBOutlet UITextField *passwordEntered;
NSString *userName;
NSMutableArray *password;
}
@property (nonatomic, retain) UITextField *usernameEntered;
@property (nonatomic, retain) UITextField *passwordEntered;
@property (nonatomic, retain) NSString *userName;
@property (nonatomic, retain) NSMutableArray *password;
- (IBAction) saveData;
- (IBAction) textFieldReturn:(id)textField;
My .m file
@implementation Register
// we use this to dismiss the keyboard when the return key is pressed
- (IBAction) textFieldReturn:(id)textField
{
[textField resignFirstResponder];
}
@synthesize userName;
@synthesize password;
@synthesize usernameEntered, passwordEntered;
- (IBAction) saveData {
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// set the variables to the values in the text fields
self.userName = usernameEntered.text;
self.password = [[NSMutableArray alloc] initWithCapacity:3];
[password addObject:passwordEntered.text];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: usernameEntered, passwordEntered, nil] forKeys:[NSArray arrayWithObjects: @"Username", @"Password", nil]];
// create dictionary with values in UITextFields
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error in saveData: %@", error);
[error release];
}
}
-(void)viewDidLoad
{
[super viewDidLoad];
// Data.plist code
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
// assign values
self.userName = [temp objectForKey:@"Username"];
self.password = [NSMutableArray arrayWithArray:[temp objectForKey:@"Password"]];
// display values
usernameEntered.text = userName;
passwordEntered.text = [password objectAtIndex:0];
}