2012-03-25 142 views
15

我有一些需要存储,访问和更新密码的Mac代码,以便将用户连接到Web API。放置这些信息的正确位置应该是Mac Keychain,但似乎没有可可接口(请参阅this answer) - 这是否仍然正确?可可接口到MacOS X钥匙扣

我看了一下苹果的Keychain documentation,API看起来非常笨重。我可以存储它并检索记录,但任何更复杂的东西似乎都需要仔细考虑可能出错的地方(请参阅this list of error codes)。

除了通过C代码进行剽窃之外,还有更好的Mac keychain接口吗?我来的最接近的是EMKeychain,但它似乎需要一些工作(例如除了吐到控制台之外没有错误处理代码)。

回答

10

你应该看看SSKeychain。作品很棒,代码真棒。

+0

这可以工作,虽然我一直在想互联网密码似乎比通用密码系统更适合我的设置。我想我可以将URL编码到SSKeychain用作标识符的“服务”中。如果我的目的没有更好的东西,至少这是一个起点,如果我想围绕互联网密码制定一个系统。 – Noah 2012-03-26 00:46:27

0

答案太迟,但对未来的帮助很有帮助。下面是我所做保存密码在钥匙串的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); 

当我们运行应用程序,以获取密码,我们将获得用户操作提示以访问钥匙串。