ios - 使用 CFDataRef 将证书保存到钥匙串(keychain)
<p><p>我正在尝试为我的 <code>SecIdentityRef</code> 身份获取 <code>CFDataRef</code> 持久引用。但是,当使用苹果提供的标准代码时,该函数为 <code>CFDataRef</code> 返回 0x0。输入参数不是 nil,但不知何故它不再起作用。代码使用完美。</p>
<pre><code>CFTypeRef persistent_ref;
CFDataRef persistentRefForIdentity(SecIdentityRef identity)
{
const void *keys[] = { kSecReturnPersistentRef, kSecValueRef };
const void *values[] = { kCFBooleanTrue, identity };
CFDictionaryRef dict = CFDictionaryCreate(NULL, keys, values, 2, NULL, NULL);
OSStatus status = SecItemAdd(dict, &persistent_ref); // `SecItemAdd` returns 0
if (dict)
CFRelease(dict);
return (CFDataRef)persistent_ref;
}
</code></pre>
<p><code>status</code> 为 0,但 <code>persistent_ref</code> 仍然没有值。</p>
<p>有人知道怎么回事吗?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使 CFTypeRef persistent_ref 成为局部变量。或者确保在调用 SecItemAdd 函数之前将其设置为 NULL。请看下面的代码。</p>
<pre><code>CFDataRef persistentRefForIdentity (SecIdentityRef identity)
{
OSStatus status = errSecSuccess;
CFTypeRefpersistent_ref = NULL;
const void *keys[] = { kSecReturnPersistentRef, kSecValueRef };
const void *values[] = { kCFBooleanTrue, identity };
CFDictionaryRef dict = CFDictionaryCreate(NULL, keys, values, 2, NULL, NULL);
// Delete anything already added
SecItemDelete(dict);
//Add the new one
status = SecItemAdd (dict, &persistent_ref);
if (status != errSecSuccess)
return nil;
if (dict)
CFRelease(dict);
return (CFDataRef)persistent_ref;
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 使用 CFDataRef 将证书保存到钥匙串(keychain),我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/25971840/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/25971840/
</a>
</p>
页:
[1]