Search code examples
objective-cmacossecuritysskeychain

SSKeychain implementation load last login and pass


I am new to objective-c. I can not understand how to do, when you start the program automatically inserts the last name that was saved in NSTextFiel and NSSecureTextField. I save the user name and password :

- (void)saveLoginPass {
    if ([checkBox state]==NSOnState) {
        [SSKeychain setPassword:self.passwordField.stringValue forService:@"ERClient" account:self.loginField.stringValue];
        NSLog(@"Login/pass save");
    }
    else if([checkBox state]==NSOffState){
        NSLog(@"Don't save login/pass");
    }
}

and

[manager POST:URLString parameters:parameter success:^(AFHTTPRequestOperation *operation, id responseObject){
        if ([operation.responseString rangeOfString:@"mainBody"].location == NSNotFound) {
            alertFild.stringValue=@"Login or pass false";
            NSLog(@"Login or pass false");
            NSLog(@"responseObject: %@", responseObject);
            NSLog(@"operation.responseString: %@",operation.responseString);
            NSLog(@"operation.response: %@",operation.response);
        } else {
            browserWindowController = [[ERBrowserWindowController alloc] initWithWindowNibName:@"ERBrowserWindowController"];
            [browserWindowController showWindow:self];
            [browserWindowController addDefaultTabs];
            [self saveLoginPass];
            [loginWindow close];
            NSLog(@"Аuthorized");
        }
    }

enter image description here

I checked in Keychain successfully stored data. How make implementation insert the final login and password when loading the program?


Solution

  • I guess you can just retrieve the username and password in the standard way.

    NSDictionary *credentials = [SSKeychain accountsForService:@"ERClient"].firstObject;
    if (!credentials) {
       return; // leave fields empty
    }
    NSString *accountName = credentialsDictionary[kSSKeychainAccountKey];
    NSString *password = [SSKeychain passwordForService:@"ERClient" account:accountName];
    // populate fields
    

    }