KeychainItem的值检索

时间:2014-05-16 23:30:19

标签: ios objective-c keychain keychainitemwrapper

我尝试从KeyChainItem中检索一个字符串,该字符串存储如下:

@property (nonatomic, strong) KeychainItemWrapper *account;
if (account == nil) {
  account = [[KeychainItemWrapper alloc] initWithIdentifier:@"test" accessGroup:nil]
       }

[account setObject:self.username forKey:(__bridge id)(kSecAttrAccount)];
[account setObject:@"c6a07d48aabf35b26e1623fb" forKey:(__bridge id)(kSecValueData)];

当我按如下方式检索时:

  KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"test" accessGroup:nil];
        self.account = wrapper;
    }

    NSString *prevUsername = [account objectForKey:(__bridge id)(kSecAttrAccount)];
    NSString *token = [account objectForKey:(__bridge id)(kSecValueData)];

我收到了NSLog的以下值(@"%@",令牌);

<63346264 32636462 64653234 34313862 38633537 31326235 66653464 63303731>

如何检索我保存的字符串?我在这里做错了吗?

1 个答案:

答案 0 :(得分:2)

试试这个:

NSData *nameData= [account objectForKey:(__bridge id)(kSecAttrAccount)];
NSData *tokenData = [account objectForKey:(__bridge id)(kSecValueData)];

NSString *prevUsername = [[NSString alloc] initWithData:nameData encoding:NSUTF8StringEncoding];

NSString *token = [[NSString alloc] initWithData:tokenData encoding:NSUTF8StringEncoding];