Question

I want to code a LoginView in Xcode, to query the Password and Username i want to use a Textfile or plist. How can i use it?

Was it helpful?

Solution

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

OTHER TIPS

Here is the lines you need to write to save the username and password to save in the PList which is in the Documents Directory..

- (IBAction)saveToPlist:(id)sender {

    NSDictionary *loginDetails=[[NSDictionary alloc]initWithObjects:@[self.userName.text,self.password.text] forKeys:@[@"username",@"password"]];


    if ([loginDetails writeToFile:[self plistPath] atomically:YES]) {
        NSLog(@"successfully entered");
    }
    else{
        NSLog(@"something wrong");
    }


}

//code to retrieve the data from plist


NSDictionary *details=[[NSDictionary alloc]initWithContentsOfFile:[self plistPath]];

where the [self plistPath] is

-(NSString*)plistPath{

    NSArray *arr=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[arr objectAtIndex:0]stringByAppendingPathComponent:@"login.plist"];
}

Happy Coding :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top