I am using the keychain to store data on the local device but have decided to adapt it for use over multiple devices via iCloud. I have enabled the iCloud entitlement and created the necessary configuration within the Member Center. However, while the data is being stored it does not appear that it is being stored in the cloud. I am testing between the simulator and my iPhone device. The simulator is logged in using my account. Each device continues to save the data but the other device does not see the results.
I only added the kSecAttrSynchronizable and kCFBooleanTrue to the exiting setup which I understood was all that was required to make the keychain use the cloud.
Here the code used for storing and recalling keychain data.
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service
{
return [NSMutableDictionary dictionaryWithObjectsAndKeys:
(__bridge id)kSecClassGenericPassword,(__bridge id)kSecClass,
service, (__bridge id)kSecAttrService,
service, (__bridge id)kSecAttrAccount,
(__bridge id)kCFBooleanTrue, (__bridge id)kSecAttrSynchronizable,
(__bridge id)kSecAttrAccessibleAfterFirstUnlock,(__bridge id)kSecAttrAccessible,
nil];
}
+ (void)save:(NSString *)service data:(id)data
{
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
CFTypeRef result = NULL;
SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(__bridge id)kSecValueData];
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)keychainQuery, &result);
if (status == errSecSuccess) NSLog(@"Succcessfully Stored Value");
else NSLog(@"Failed to store value with code: %ld",(long)status);
}
+ (id)load:(NSString *)service {
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(__bridge id)kSecReturnData];
[keychainQuery setObject:(__bridge id)kSecMatchLimitOne forKey:(__bridge id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((__bridge CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(__bridge NSData *)keyData];
}
@catch (NSException *e) {
NSLog(@"Unarchive of %@ failed: %@", service, e);
}
@finally {}
}
if (keyData) CFRelease(keyData);
return ret;
}
+ (void)delete:(NSString *)service {
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((__bridge CFDictionaryRef)keychainQuery);
}