2012-03-25 29 views
15

Tôi có một chút mã Mac cần lưu trữ, truy cập và cập nhật mật khẩu để kết nối người dùng với API web. Đúng nơi để đưa thông tin này nên là Mac Keychain, nhưng có vẻ như không phải là một giao diện cacao (xem this answer) - điều này vẫn đúng?Giao diện cacao cho MacOS X Keychain

Tôi đã xem xét số Keychain documentation của Apple và API có vẻ cực kỳ khó chịu. Tôi có thể lưu trữ nó và lấy hồ sơ, nhưng bất cứ điều gì phức tạp hơn dường như đòi hỏi rất nhiều suy nghĩ như những gì có thể đi sai (xem this list of error codes).

Có giao diện tốt hơn cho móc khóa Mac, ngoài việc làm tắc nghẽn thông qua mã C không? Gần nhất tôi đã đến là EMKeychain nhưng có vẻ như nó cần một chút công việc (ví dụ: không có mã xử lý lỗi ngoài việc nhổ vào bảng điều khiển).

Trả lời

10

Bạn nên xem SSKeychain. Hoạt động tuyệt vời, mã tuyệt vời.

+0

Điều này có thể hoạt động, mặc dù tôi đã nghĩ mật khẩu internet có vẻ tốt hơn một chút so với kết hợp với thiết lập của tôi so với hệ thống mật khẩu chung. Tôi đoán tôi có thể mã hóa url vào "dịch vụ" mà SSKeychain sử dụng làm định danh. Nếu không có bất cứ điều gì tốt hơn cho mục đích của tôi, ít nhất đây là một điểm khởi đầu nếu tôi muốn đặt cùng một hệ thống xung quanh các mật khẩu internet. – Noah

0

Câu trả lời quá muộn nhưng sẽ tốt cho trợ giúp trong tương lai. Dưới đây là những gì tôi đã làm để lưu mật khẩu trong Keychain của 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]]; 
} 

Đối với tiết kiệm mật khẩu

- (IBAction)saveButtonClicked:(id)sender { 
    [self createCredentialForUsername:@"User_Name" Password:@"Your_Pass"]; 
} 

cho việc lấy lại mật khẩu

NSURLCredential *credential; 
NSDictionary *credentials; 
credentials = [[NSURLCredentialStorage sharedCredentialStorage] credentialsForProtectionSpace:[self createProtectionSpaceForBasicAuthentication]]; 
credential = [credentials.objectEnumerator nextObject]; 
    NSLog(@"Username: %@ and password %@", credential.user, credential.password); 

Khi chúng tôi chạy ứng dụng để lấy mật khẩu, chúng tôi sẽ nhận được lời nhắc hành động người dùng để truy cập vào keychain.

Các vấn đề liên quan