2014-05-16 76 views
0

我正在尝试使用KeyChain并且没有包装。但是当我想读取值时,我的代码崩溃了。iOS KeyChain secItemAdd crash

代码:

CFDictionaryRef attributes = CFDictionaryCreate(NULL, keys, values, 5, NULL, NULL); 

CFDataRef result; 
OSStatus status = SecItemAdd(attributes, (CFTypeRef *)&result); 
if (status == errSecSuccess) { 
    if (result && CFGetTypeID(result) == CFDataGetTypeID()) { //crashes here 
    NSLog(@"Data"); 

    } 


    isSuccess = YES; 
} else { 
    fprintf(stderr, "Error while inserting into keychain osstatus:%ld\n", status); 
} 

错误:EXC_BAD_ACCESS

我在做什么错?我想SecItemAdd可以返回新添加的项

编辑:

const void *keys[] = { 
    kSecClass 
    , kSecAttrAccessible 
    , kSecAttrService 
    , kSecAttrAccount 
    , kSecValueData 
}; 

const void *values[] = { 
    kSecClassGenericPassword 
    , kSecAttrAccessibleWhenUnlocked 
    , (__bridge CFStringRef)service 
    , (__bridge CFStringRef)account 
    , data //CFDataRef 
}; 
+0

什么是 “钥匙” 和 “价值观” 声明? –

+0

添加到问题 – Haagenti

回答

2

从技术文档:

To obtain the data of the added item as an object of type CFDataRef, specify the return type key kSecReturnData with a value of kCFBooleanTrue.

+0

我认为这只需要在SecItemCopyMatching上完成。谢谢! – Haagenti

-1

这个问题的答案,为什么你要EXC_BAD_ACCESS,是因为您传递到字典SecItemAdd必须是可变的。

尝试这样:

CFMutableDictionaryRef attributes = CFDictionaryCreateMutable(NULL, size, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);

CFDictionaryAddValue(attributes, keys[x], values[x]);

相关问题