iOS - Keychain - Lockbox,首次启动时不保存数组

时间:2012-11-16 20:24:16

标签: objective-c ios xcode

我可能做错了,但是当我使用下面的代码,并希望Lockbox在第一次启动应用程序时将元素保存到钥匙串时,不会存储任何内容。如果我再次运行应用程序并在else部分中添加元素,则会保存数据。

有什么建议吗?

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);

}

1 个答案:

答案 0 :(得分:0)

公顷!得到它..起初没有看到它:

第一次,nothin存储在钥匙串中,因此数组是NIL

值应为[nil componentsJoinedBy ...]

值为零!

所以[nil stringByAppendinString将始终为nil并且首次运行时将保存EMPTY数组


第二次运行数组上的

不是nil,因为Lockbox读回空数组 (那么一切都有效)

想法,改变:

NSArray *array = [Lockbox arrayForKey:@"TestKey"];
value = [array componentsJoinedByString:@"|"];

if(value==nil)
...

NSArray *array = [Lockbox arrayForKey:@"TestKey"];
if(array)
     value = [array componentsJoinedByString:@"|"];
else
     value = @"";

if(value.length)
...