Search code examples
iosios7iclouduuidkeychain

Keychain icloud support after data has already been stored?


I wonder what's going to happen when the user hasn't turned on the iCloud Keychain support (the iOS default?) on iOS 7 and then, the following code is ran and after that, the iCloud Keychain support is turned on.

    KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.joseandro.UUID"  accessGroup:nil];

    CFUUIDRef theUUID = CFUUIDCreate(NULL);
    CFStringRef string = CFUUIDCreateString(NULL, theUUID);
    CFRelease(theUUID);

    NSString* UUID = [(NSString *)string autorelease];

    [keychain setObject:@"UNIQUE_IDS_SERVICE" forKey:kSecAttrService];
    [keychain setObject:@"DeviceUUID" forKey:kSecAttrAccount];
    [keychain setObject:UUID forKey:kSecValueData];
    [keychain setObject:kSecAttrAccessibleAlwaysThisDeviceOnly forKey:kSecAttrAccessible];

    [keychain release];
    keychain = nil;

The previously stored "DeviceUUID" value data is going to be used, or the Keychain is completely erased so the returned value would be empty (with the following code) and the I'd lose what was stored before all that to happen?

   KeychainItemWrapper* keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.joseandro.UUID"  accessGroup:nil];

   NSString* UUID = [keychain objectForKey:(kSecValueData)];
   return UUID;

Solution

  • It doesn't matter when the iCloud Keychain support is turned on, the key is always going to be there once it's stored.

    I just couldn't verify the behavior when the iCloud hasn't been activated and the OS is erased and installed back again.