gpt4 book ai didi

ios - iOS 中的钥匙串(keychain)访问 : "Keychain failed to store the value with code: -50"

转载 作者:行者123 更新时间:2023-12-01 18:48:20 26 4
gpt4 key购买 nike

我在调用 SecItemAdd 时没有找到有关此类错误代码的信息,可能是什么原因造成的?

提前致谢

编辑:这是我得到错误的函数:

+ (BOOL)storeWithKey:(NSString *)keyStr withValueStr:(NSString *)valueStr
{
if ((keyStr != nil) && (![keyStr isEqualToString:@""]) &&
(valueStr != nil) && (![valueStr isEqualToString:@""])) {

NSData *valueData = [valueStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *service = [[NSBundle mainBundle] bundleIdentifier];

NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassInternetPassword,
(__bridge id)kSecAttrService : service,
(__bridge id)kSecAttrAccount : keyStr,
(__bridge id)kSecValueData : valueData};

CFTypeRef result = NULL;

// Store value and get the result code
OSStatus status = SecItemAdd((__bridge CFDictionaryRef)secItem, &result);

NSLog(@"'writeToKeychain'. %@", [self getErrorMessage:status]);

return [self checkIfInKeychain:status];
}
else {
return NO;
}
}

最佳答案

-50表示 One or more parameters passed to a function were not valid .你错误的参数组合。

如果您使用 kSecAttrServicekSecAttrAccount , kSecClass应该是 kSecClassGenericPassword .

NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService : service,
(__bridge id)kSecAttrAccount : keyStr,
(__bridge id)kSecValueData : valueData};

如果您使用 kSecClassInternetPasswordkSecClass , 你应该使用 kSecAttrServerkSecAttrPort (如果需要)改为 kSecAttrService .
NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassInternetPassword,
(__bridge id)kSecAttrServer : @"example.com",
(__bridge id)kSecAttrPort : @(80), // Optional
(__bridge id)kSecAttrAccount : keyStr,
(__bridge id)kSecValueData : valueData};

关于ios - iOS 中的钥匙串(keychain)访问 : "Keychain failed to store the value with code: -50",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33213271/

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