繁体   English   中英

iOS中的Keychain访问:“Keychain无法使用代码存储值:-50”

[英]Keychain access in iOS: “Keychain failed to store the value with code: -50”

在调用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};

如果使用kSecClassInternetPassword作为kSecClass ,则应使用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};

错误代码的文档位于Security/SecBase.h文件中。 你可以在最后找到它们。

您还可以在SecItemAdd的文档中找到它们。 https://developer.apple.com/library/ios/documentation/Security/Reference/keychainservices/#//apple_ref/doc/uid/TP30000898-CH5g-CJBEABHG

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM