I've got a bit of Mac code that needs to store, access and update passwords in order to connect users with a web API. The right place to put this information should be the Mac Keychain, but there doesn't seem to be a cocoa interface (see this answer) -- is this still correct?
我有一些Mac代码需要存储,访问和更新密码才能将用户与Web API连接起来。放置此信息的正确位置应该是Mac Keychain,但似乎没有cocoa接口(请参阅此答案) - 这仍然是正确的吗?
I've looked at Apple's Keychain documentation, and the API seems incredibly clunky. I can store to it and retrieve records, but anything more complex seems to require a lot of thought as to what might go wrong (see this list of error codes).
我看过Apple的Keychain文档,API看起来非常笨重。我可以存储并检索记录,但任何更复杂的东西似乎都需要考虑可能出错的地方(请参阅此错误代码列表)。
Is there a better interface to the Mac keychain, aside from slogging through the C code? The closest I've come is EMKeychain but it seems like it needs a bit of work (e.g. no error handling code aside from spitting to the console).
除了通过C代码之外,还有更好的Mac钥匙串接口吗?我最接近的是EMKeychain,但它似乎需要一些工作(例如,除了随地吐痰到控制台之外没有错误处理代码)。
3 个解决方案
#2
0
Too late answer but would be good for future help. Below is what I did to save the password in Keychain of Mac
答案太晚,但对未来的帮助会有好处。以下是我在Mac Keychain中保存密码的操作
#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]];
}
For saving password
用于保存密码
- (IBAction)saveButtonClicked:(id)sender {
[self createCredentialForUsername:@"User_Name" Password:@"Your_Pass"];
}
for fetching the password
用于获取密码
NSURLCredential *credential;
NSDictionary *credentials;
credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[self createProtectionSpaceForBasicAuthentication]];
credential = [credentials.objectEnumerator nextObject];
NSLog(@"Username: %@ and password %@", credential.user, credential.password);
When we run the app to fetch password, we will get user action prompt for keychain access.
当我们运行应用程序以获取密码时,我们将获得用户操作提示以进行密钥链访问。
#3
0
Late to the party, as always, but I can recommend UICKeyChainStore.
一如既往的晚会,但我可以推荐UICKeyChainStore。
#1
#2
0
Too late answer but would be good for future help. Below is what I did to save the password in Keychain of Mac
答案太晚,但对未来的帮助会有好处。以下是我在Mac Keychain中保存密码的操作
#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]];
}
For saving password
用于保存密码
- (IBAction)saveButtonClicked:(id)sender {
[self createCredentialForUsername:@"User_Name" Password:@"Your_Pass"];
}
for fetching the password
用于获取密码
NSURLCredential *credential;
NSDictionary *credentials;
credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[self createProtectionSpaceForBasicAuthentication]];
credential = [credentials.objectEnumerator nextObject];
NSLog(@"Username: %@ and password %@", credential.user, credential.password);
When we run the app to fetch password, we will get user action prompt for keychain access.
当我们运行应用程序以获取密码时,我们将获得用户操作提示以进行密钥链访问。
#3
0
Late to the party, as always, but I can recommend UICKeyChainStore.
一如既往的晚会,但我可以推荐UICKeyChainStore。