答案太迟,但对未来的帮助很有帮助。下面是我所做保存密码在钥匙串的Mac
#pragma -mark Password save in Keychain
-(NSURLProtectionSpace *)createProtectionSpaceForBasicAuthentication{
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost:@"http://yourURLHere"
port:1804 //add Your port here
protocol:@"http" //can be pass as nil
realm:nil
authenticationMethod:NSURLAuthenticationMethodHTTPBasic];
return protectionSpace;
}
-(void)createCredentialForUsername:(NSString *)username Password:(NSString *)password{
NSURLCredential *credentialObject;
credentialObject = [NSURLCredential credentialWithUser:username password:password persistence:NSURLCredentialPersistencePermanent];
[[NSURLCredentialStorage sharedCredentialStorage] setCredential:credentialObject forProtectionSpace:[self createProtectionSpaceForBasicAuthentication]];
}
的用于保存密码
- (IBAction)saveButtonClicked:(id)sender {
[self createCredentialForUsername:@"User_Name" Password:@"Your_Pass"];
}
为取回密码
NSURLCredential *credential;
NSDictionary *credentials;
credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[self createProtectionSpaceForBasicAuthentication]];
credential = [credentials.objectEnumerator nextObject];
NSLog(@"Username: %@ and password %@", credential.user, credential.password);
当我们运行应用程序,以获取密码,我们将获得用户操作提示以访问钥匙串。
这可以工作,虽然我一直在想互联网密码似乎比通用密码系统更适合我的设置。我想我可以将URL编码到SSKeychain用作标识符的“服务”中。如果我的目的没有更好的东西,至少这是一个起点,如果我想围绕互联网密码制定一个系统。 – Noah 2012-03-26 00:46:27