我使用下面的代码在iOS上安全地存储字符串。有时SecItemAdd()
然后-34018
失败并返回错误代码 NSMutableDictionary* query = [NSMutableDictionary dictionary];
[query setObject:(__bridge id)kSecClassGenericPassword forKey:(__bridge id)kSecClass];
[query setObject:account forKey:(__bridge id)kSecAttrAccount];
[query setObject:(__bridge id)kSecAttrAccessibleAlways forKey:(__bridge id)kSecAttrAccessible];
OSStatus error = SecItemCopyMatching((__bridge CFDictionaryRef)query, NULL);
if (error == errSecSuccess)
{
// Do update.
NSDictionary* attributesToUpdate;
attributesToUpdate = [NSDictionary dictionaryWithObject:[inputString dataUsingEncoding:NSUTF8StringEncoding]
forKey:(__bridge id)kSecValueData];
error = SecItemUpdate((__bridge CFDictionaryRef)query, (__bridge CFDictionaryRef)attributesToUpdate);
if (error != errSecSuccess)
{
NBLog(@"SecItemUpdate failed: %d", (int)error);
result = NO;
}
}
else if (error == errSecItemNotFound)
{
// Do add.
[query setObject:[inputString dataUsingEncoding:NSUTF8StringEncoding] forKey:(__bridge id)kSecValueData];
error = SecItemAdd((__bridge CFDictionaryRef)query, NULL);
if (error != errSecSuccess)
{
NBLog(@"SecItemAdd failed: %d", (int)error);
result = NO;
}
}
。我找不到这个代码;这是什么意思?
GS a
答案 0 :(得分:0)
-34018
是errSecMissingEntitlement
Internal error when a required entitlement isn't present.
对于iOS 10或更高版本,您需要一个.entitlements
文件来访问钥匙串。像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>com.apple.keystore.access-keychain-keys</key>
<true/>
<key>com.apple.keystore.device</key>
<true/>
</dict>
</plist>