iOS中的钥匙串访问:“钥匙串无法将代码存储在代码中:-50”
调用SecItemAdd
时,我没有找到有关此类错误代码的信息,可能是什么原因造成的?iOS中的钥匙串访问:“钥匙串无法将代码存储在代码中:-50”
在此先感谢
编辑:这是我的错误的功能:
+ (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
。你是错误的参数组合。
如果您使用kSecAttrService
和kSecAttrAccount
,kSecClass
应该是kSecClassGenericPassword
。
NSDictionary *secItem = @{(__bridge id)kSecClass : (__bridge id)kSecClassGenericPassword,
(__bridge id)kSecAttrService : service,
(__bridge id)kSecAttrAccount : keyStr,
(__bridge id)kSecValueData : valueData};
如果使用kSecClassInternetPassword
为kSecClass
,你应该使用kSecAttrServer
和kSecAttrPort
(如果需要),而不是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};
这解决了我在Swift 3中的一个类似问题。在没有kSecClass的ObjC中工作,但在Swift 3中,它给出了-50。添加kSecClass与kSecClassGenericPassword,它的工作原理。谢谢@ kishikawa-katsumi – Brett
的错误代码是在Security/SecBase.h
文件的文档。你可以在最后找到它们。
你也可以在文档中找到它们,以SecItemAdd
。 https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/#//apple_ref/doc/uid/TP30000898-CH5g-CJBEABHG
你在哪里得到这个错误? –
-50 = paramErr。呼叫中的某些参数是错误的,由于参数不正确,呼叫不可能成功。 – gnasher729