Cocoa interface to MacOS X Keychain
Asked Answered
A

3

16

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?

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).

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).

Apteryx answered 25/3, 2012 at 21:27 Comment(0)
N
10

You should take a look at SSKeychain. Works great, awesome code.

Newborn answered 25/3, 2012 at 21:30 Comment(1)
This could work, although I've been thinking the internet password seems a bit better of a match to my setup than the generic password system. I guess I could encode the url into the "service" that SSKeychain uses as an identifier. If there isn't anything better for my purposes, at least this is a starting point if I want to put together a system around the internet passwords.Apteryx
R
0

Too late answer but would be good for future help. Below is what I did to save the password in Keychain of 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]];
}

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.

Redskin answered 28/7, 2017 at 13:0 Comment(0)
P
0

Late to the party, as always, but I can recommend UICKeyChainStore.

Pecos answered 12/7, 2018 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.