objective-c 我可以在 iPhone 上访问钥匙串吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/190963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I access the keychain on the iPhone?
提问by DavidM
This question discusses encrypting data on the iPhoneusing the crypt() function. As an alternative, is there a keychain on the iPhone and if so, what code would I use to access it in order to store login details and then retrieve them for us in an application?
这个问题讨论了使用 crypt() 函数在 iPhone 上加密数据。作为替代方案,iPhone 上是否有钥匙串,如果有,我将使用什么代码来访问它以存储登录详细信息,然后在应用程序中为我们检索它们?
回答by Ben Gottlieb
One other thing to note: the keychain APIs don't work in the simulator when using older versions (2.x, 3.x) of the iPhone SDK. This could save you a lot of frustration when testing!
需要注意的另一件事是:当使用旧版本(2.x、3.x)的 iPhone SDK 时,钥匙串 API 在模拟器中不起作用。这可以在测试时为您节省很多挫折!
回答by Adam Byram
There is a keychain you can use - for code, the best bet is to check out the GenericKeychain sample application from Apple:
您可以使用一个钥匙串 - 对于代码,最好的办法是查看 Apple 的 GenericKeychain 示例应用程序:
回答by AlBeebe
Here is what i use to store Key/Value pairs in the keychain. Make sure to add Security.framework to your project
这是我用来在钥匙串中存储键/值对的内容。确保将 Security.framework 添加到您的项目中
#import <Security/Security.h>
// -------------------------------------------------------------------------
-(NSString *)getSecureValueForKey:(NSString *)key {
/*
Return a value from the keychain
*/
// Retrieve a value from the keychain
NSDictionary *result;
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecReturnAttributes, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, kCFBooleanTrue, nil] autorelease];
NSDictionary *query = [[NSDictionary alloc] initWithObjects: objects forKeys: keys];
// Check if the value was found
OSStatus status = SecItemCopyMatching((CFDictionaryRef) query, (CFTypeRef *) &result);
[query release];
if (status != noErr) {
// Value not found
return nil;
} else {
// Value was found so return it
NSString *value = (NSString *) [result objectForKey: (NSString *) kSecAttrGeneric];
return value;
}
}
// -------------------------------------------------------------------------
-(bool)storeSecureValue:(NSString *)value forKey:(NSString *)key {
/*
Store a value in the keychain
*/
// Get the existing value for the key
NSString *existingValue = [self getSecureValueForKey:key];
// Check if a value already exists for this key
OSStatus status;
if (existingValue) {
// Value already exists, so update it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemUpdate((CFDictionaryRef) query, (CFDictionaryRef) [NSDictionary dictionaryWithObject:value forKey: (NSString *) kSecAttrGeneric]);
} else {
// Value does not exist, so add it
NSArray *keys = [[[NSArray alloc] initWithObjects: (NSString *) kSecClass, kSecAttrAccount, kSecAttrGeneric, nil] autorelease];
NSArray *objects = [[[NSArray alloc] initWithObjects: (NSString *) kSecClassGenericPassword, key, value, nil] autorelease];
NSDictionary *query = [[[NSDictionary alloc] initWithObjects: objects forKeys: keys] autorelease];
status = SecItemAdd((CFDictionaryRef) query, NULL);
}
// Check if the value was stored
if (status != noErr) {
// Value was not stored
return false;
} else {
// Value was stored
return true;
}
}
It is worth noting that these key/values will not get deleted if the user deletes your app. If a user deletes your app, then reinstalls it, the key/values will still be accessible.
值得注意的是,如果用户删除您的应用程序,这些键/值不会被删除。如果用户删除您的应用程序,然后重新安装它,键/值仍然可以访问。
回答by bbrown
I really like Buzz Anderson's Keychain abstraction layerand I eagerly await Jens Alfke's MYCryptoto reach a usable state. The latter does a competent job of allowing use on Mac OS X and the iPhone using the same code, though its features only mimic a small subset of the Keychain's.
我真的很喜欢Buzz Anderson 的 Keychain 抽象层,我热切地等待Jens Alfke 的 MYCrypto达到可用状态。后者可以使用相同的代码在 Mac OS X 和 iPhone 上使用相同的代码,尽管它的功能只是模仿钥匙串的一小部分。
回答by whoisjake
Also remember that when generating an AppID, if you want more than one application to access the same Keychain information, you have to generate a wild card AppID (#####.com.prefix.*)...
还要记住,在生成AppID时,如果要多个应用访问同一个Keychain信息,就得生成一个通配符AppID(#####.com.prefix.*)...
回答by joobik
With the latest version 1.2 of the GenericKeychain sample Apple provides a keychain wrapper that also runs on the iPhone Simulator. Check out at this article for details: http://dev-metal.blogspot.com/2010/08/howto-use-keychain-in-iphone-sdk-to.html
对于 GenericKeychain 示例的最新版本 1.2,Apple 提供了一个也可以在 iPhone 模拟器上运行的钥匙串包装器。查看本文了解详情:http: //dev-metal.blogspot.com/2010/08/howto-use-keychain-in-iphone-sdk-to.html
回答by Bhavin Kansagara
Here is one more good wrapper class from Mr.Granoff https://github.com/granoff/LockboxThanks
这是 Mr.Granoff https://github.com/granoff/Lockbox 的另一个很好的包装类 谢谢

