0

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];

}

1 Answers1

1

NSDictionary includes a method called writeToFile:atomically: that just writes your dictionary to the plist automatically so you don't have to do it yourself. You can change your saveData method to

-(void)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
    [plistDict writeToFile:plistPath atomically:YES];    // write data to plist
}

And change your viewDidLoad to the following:

-(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"];
}

NSDictionary *temp = [NSDictionary dictionaryWithContentsOfFile:plistPath];
if (!temp)
{
    NSLog(@"Error reading plist.");
}
// assign values
self.userName = [temp objectForKey:@"Username"];
self.password = [NSMutableArray arrayWithArray:[temp objectForKey:@"Password"]];
// display values
usernameEntered.text = userName;
passwordEntered.text = [password objectAtIndex:0];
}

You also might want to look into encrypting the password. For more information, see here: AES Encryption for an NSString on the iPhone

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
NobodyNada
  • 7,529
  • 6
  • 44
  • 51
  • Thank you. Now how would i check the data to see if it is correct and to do a method to change scenes. Just put the if statement. i can put the change scenes part. – user3476583 Mar 30 '14 at 16:14
  • `if ([passwordEntered.text isEqualToString:correctPassword]) [self changeScenes];` – NobodyNada Mar 30 '14 at 21:03