SSKeychain实现加载上次登录并传递

时间:2015-03-20 22:59:11

标签: objective-c macos security sskeychain

我是Objective-c的新手。 我无法理解怎么做,当你启动程序时自动插入保存在NSTextFiel和NSSecureTextField中的姓氏。 我保存了用户名和密码:

- (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");
    }
}

[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

我在Keychain中检查了成功存储的数据。 加载程序时如何使实现插入最终的登录名和密码?

1 个答案:

答案 0 :(得分:0)

我想你可以用标准的方式检索用户名和密码。

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

}