答案太遲,但對未來的幫助很有幫助。下面是我所做保存密碼在鑰匙串的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