iosobjective-ckeychainkeychainitemwrapper

Value retrieval from KeychainItem


I tried retrieving a string from the KeyChainItem, which is stored as below:

@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)];

When I retrieved it as below:

  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)];

I received the following value for NSLog(@"%@",token);

<63346264 32636462 64653234 34313862 38633537 31326235 66653464 63303731>

How do I retreive the string that I saved? Am I doing anything wrong here?


Solution

  • Try this:

    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];