gpt4 book ai didi

ios - SecAddItem 只返回 errSecParam,不管我做什么

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:46:02 26 4
gpt4 key购买 nike

出于某种原因,我无法使用这个简单的钥匙串(keychain)代码。

//Let's create an empty mutable dictionary:
NSMutableDictionary *keychainItem = [NSMutableDictionary dictionary];

NSString *username = self.nwUsernameTxtFld.text;
NSString *password = self.passwordTxtFld.text;

//Populate it with the data and the attributes we want to use.

keychainItem[(__bridge id)kSecClass] = (__bridge id)kSecClassInternetPassword; // We specify what kind of keychain item this is.
keychainItem[(__bridge id)kSecAttrAccessible] = (__bridge id)kSecAttrAccessibleWhenUnlocked; // This item can only be accessed when the user unlocks the device.
keychainItem[(__bridge id)kSecAttrServer] = @"http://www.myawesomeservice.com";
keychainItem[(__bridge id)kSecAttrAccount] = username; //Our username.

if(SecItemCopyMatching((__bridge CFDictionaryRef)keychainItem, NULL) == noErr)
{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"The Item Already Exists", nil)
message:NSLocalizedString(@"Please update it instead.", )
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", nil)
otherButtonTitles:nil];
[alert show];

}else
{
NSLog(@"can add new item.");
keychainItem[(__bridge id)kSecValueData] = password; //Our password

OSStatus sts = SecItemAdd((__bridge CFDictionaryRef)keychainItem, NULL);
NSLog(@"%d", (int)sts);
}

我已经检查了 Apple 关于此事的文档,并根据它:

One or more parameters passed to the function were not valid.

然而,SecAddItem 函数有两个参数:

OSStatus SecItemAdd (
CFDictionaryRef attributes,
CFTypeRef *result
);

我将 NULL 传递给结果。根据 Apple 的文档,这是可以接受的:

result On return, a reference to the newly added items. The exact type of the result is based on the values supplied in attributes, as discussed below. Pass NULL if this result is not required.

所以我真的不知道我在这里做错了什么。我正在向它传递一个字典和一个 NULL 值。该函数需要两者,而我两者都给了它。有人可以告诉我哪里出了问题吗?

作为引用,文档为 here .

最佳答案

您正在为键 kSecValueData 向字典中插入不正确的数据类型。如问题底部的文档所示,它需要是 CFDataRef (或桥接 NSData),但您插入的是 NSString .将 NSString 转换为 NSData 是一件微不足道的事情,一旦这样做,您将不再出现此错误。

关于ios - SecAddItem 只返回 errSecParam,不管我做什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24153605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com