I am probably doing something wrong, but when I use the code below, and want Lockbox to save elements to the keychain on the first launch of the app nothing is stored. If I then run the app again and add the elements in the else part, then it saves the data.
Any suggestions?
NSString *value = @"";
NSArray *array = [Lockbox arrayForKey:@"TestKey"];
value = [array componentsJoinedByString:@"|"];
if(value == nil)
{
BOOL result = NO;
NSArray *narray = [NSArray arrayWithObjects:
[value stringByAppendingString:@"key1"],
[value stringByAppendingString:@"key2"],
[value stringByAppendingString:@"|http://www.url.com/"],
[value stringByAppendingString:@"|http://www.url2.com/"],nil];
result = [Lockbox setArray:narray forKey:@"TestKey"];
}else{
NSArray *narray = [NSArray arrayWithObjects:
[value stringByAppendingString:@"key1"],
[value stringByAppendingString:@"key2"],
[value stringByAppendingString:@"|http://www.url.com/"],
[value stringByAppendingString:@"|http://www.url2.com/"],nil];
result = [Lockbox setArray:narray forKey:@"TestKey"];
NSString *keyv1 =[array objectAtIndex:0];
NSLog(@"key value %@",keyv1);
}
ha! got it .. didnt see it at first:
the first time, nothin is stored in the keychain so array is NIL
value should then be [nil componentsJoinedBy…]
value is nil!
so [nil stringByAppendinString will always be nil and an EMPTY array will be saved on first run
on second run array is NOT nil as Lockbox reads back the empty array
(so then all works)
idea, change:
to