2014-05-16 77 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]);

相關問題