문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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 :)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top