Kelly Anne
Kelly Anne

Reputation: 1

Keychain API in ObjectiveC, not C?

I'm new to Cocoa and ObjectiveC and I'm writing a basic programme that uses a web service to login a user in to a website. I want to store the credentials in the Keychain and I've come across SecKeychainFindGenericPassword in the Keychain API. It's exactly what I need, but the example is written in C, not ObjectiveC!

I've tried invoking it like this: [SecKeychainFindGenericPassword .. and the autocomplete suggests all the parameters, but the types are const char , not NSString!

Can this function be used in ObjectiveC too? Can someone please show me a line of ObjectiveC that calls this function?

Thank you!

x

Upvotes: 0

Views: 1665

Answers (1)

Joe
Joe

Reputation: 57179

Yes the function can be used but I would recommend using an existing Objective-C keychain wrapper. When you need a C string from an NSString the easiest way is to use the UTF8String property.

const char *utf8str = [@"nsstring" UTF8String];

If you would like to specify a different encoding use - (const char *)cStringUsingEncoding:(NSStringEncoding)encoding

For an example a quick google search brought up this example.

error = SecKeychainFindGenericPassword(keychain, strlen([service UTF8String]), [service UTF8String], strlen([account UTF8String]), [account UTF8String], &existingPasswordLength, (void **)&existingPasswordData, &existingItem);

Upvotes: 3

Related Questions