Parolayı kaydetmek için anahtar olarak kSecValue verilerini kullanmalısınız (NSData veya CFDataRef biçiminde).
Referans, bu konuda biraz belirsizdir, kSecValueData anahtarı, giriş anahtarı kadar çıktı anahtarı olarak da çalışır. Yani, bir anahtarlık öğesi (SecItemCopyMatching) sorguladığınızda ve bir kSecReturnAttributes anahtarı belirttiğinizde kullanırsınız, sonuç bir sözlük olarak döndürülür, parola bu sözlüğün kSecValueData anahtarı altında depolanır. Ayrıca, anahtarlık (SecItemAdd) öğesine bir öğe eklediğinizde, yöntemi çağırmadan önce kSecValueData anahtarındaki parolanızın NSData veya CFDataRef değerini sakladığınızda da kullanabilirsiniz.
alınıyor şifre:
NSMutableDictionary *queryDictionary = [[NSMutableDictionary alloc] init];
[queryDictionary setObject: (__bridge id)kSecClassGenericPassword forKey: (__bridge id<NSCopying>)kSecClass];
[queryDictionary setObject:service forKey:kSecAttrService];
[queryDictionary setObject:account forKey:kSecAttrAccount];
// The result will be a dictionary containing the password attributes...
[queryDictionary setObject:YES forKey:(__bridge id<NSCopying>)(kSecReturnAttributes)];
// ...one of those attributes will be a kSecValueData with the password
[queryDictionary setObject:YES forKey:(__bridge id<NSCopying>)(kSecReturnData)];
OSStatus sanityCheck = SecItemCopyMatching((__bridge CFDictionaryRef)(queryDictionary), (CFTypeRef *)&result);
if (sanityCheck != noErr)
{
NSDictionary * resultDict = (__bridge NSDictionary *)result;
// here's the queried password value
NSData *passwordValue = [resultDict objectForKey:(__bridge id)(kSecValueData)];
}
ekleme şifresi:
NSString *passwordString = @"my password value";
NSData *passwordData = [passwordString dataUsingEncoding:NSUTF8StringEncoding];
CFDictionaryRef result = nil;
NSMutableDictionary *addDictionary = [[NSMutableDictionary alloc] init];
[addDictionary setObject: (__bridge id)kSecClassGenericPassword forKey: (__bridge id<NSCopying>)kSecClass];
[addDictionary setObject:service forKey:kSecAttrService];
[addDictionary setObject:account forKey:kSecAttrAccount];
// here goes the password value
[addDictionary setObject:passwordData forKey:(__bridge id<NSCopying>)(kSecValueData)];
OSStatus sanityCheck = SecItemAdd((__bridge CFDictionaryRef)(queryDictionary), NULL)
if (sanityCheck != noErr)
{
// if no error the password got successfully stored in the keychain
}
Daha iyi kod, burada daha az hata ile: Burada
iki durumda bir örnek http://stackoverflow.com/questions/19284063/secitemcopymatching-returns-nil-data –