Reputation: 281
I am trying to make a simple password protected app using a text file to store the password that the user entered. I want to take whats in a text field store it in a file and ultimately compare whats in that file to what the user enters in another text field. here is what I have:
//Setting the string to hold the password the user has entered
NSString *createPassword1 = passwordSet.text;
//creating a muttable array to store the value of createPassword1
NSMutableArray *passwordArray = [NSMutableArray array];
//storing createpassword1 into the first element of the array
[passwordArray addObject:createPassword1];
NSLog(@"%@",[passwordArray objectAtIndex:0]);//seeing if it is stored correctly (it is)
//path for searching for the file
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//my filename
NSString *fileName = @"PasswordFile.txt";
NSString *fileAndPath = [path stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileAndPath]) {
[[NSFileManager defaultManager] createFileAtPath:fileAndPath contents:nil attributes:nil];
}
[[[passwordArray objectAtIndex:0] dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAndPath atomically:YES];
Any help will be greatly appreciated thank you.
Upvotes: 0
Views: 95
Reputation: 4520
What you do is too complicated. Why do you use a NSMutableArray ("passwordArray") to store a single password? Why do you convert it to NSData and write this to a file? Just use a string and use its writeToFile method. Alternatively use NSArray's writeToFile method.
Alternatively, and my personal favorite: use NSUSerDefaults à la:
[[NSUserDefaults standardUserDefaults] setValue: myPasswordString forKey:@"appPassword"]];
EDIT in response to some comments: The above only applies if used in a "trivial" app that needs password-protection in a very low-level manner. Anything to protect really sensitive data should be handled differently. The original poster explicitly stated
I want to take whats in a text field store it in a file and ultimately compare whats in that file to what the user enters in another text field.
So one can assume that high-level security is not an issue here.
Upvotes: 1