Generally passwords are either taken from service call or from userdefaults:
You can do something as :
- (IBAction)submitPressed:(id)sender {
NSString *userName = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
if (userName == NULL) {//no user pwd exists. create a new one
if (self.userTextField.text.length>0 && self.passwordTextField.text.length>0) {
NSLog(@"Create a user+pwd in server. Now for showing purpose we are saving it in userdefaults.");
[[NSUserDefaults standardUserDefaults] setObject:self.userTextField.text forKey:@"userName"];
[[NSUserDefaults standardUserDefaults] setObject:self.passwordTextField.text forKey:@"password"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
else{ //some user pwd exists, now check that with json value.
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"password"];
if ([userName isEqualToString:self.userTextField.text] &&
[password isEqualToString:self.passwordTextField.text]) {
NSLog(@"logged in successfully");
//[self performSegueWithIdentifier:@"HomeView" sender:nil];
}
else{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Login"
message:@"Invalid username and/or password"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alertView show];
}
}
}
Edit:
If you still want to read user/password from text file of plist you can use :
//PLIST
NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Credentials" ofType:@"plist"]];
NSString *user = dictionary[@"username"];
NSString *password = dictionary[@"password"];
//TEXT
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Credentials" ofType:@"txt"];
NSString *contents = [NSString stringWithContentsOfFile:filePath
encoding:NSUTF8StringEncoding
error:NULL];
//then use NSString api's to extract user and password from contents