2012-03-25 143 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); 

當我們運行應用程序,以獲取密碼,我們將獲得用戶操作提示以訪問鑰匙串。