سؤال

I am currently using this version of a KeyChain Wrapper for my iOS app. However, I am having difficulty adding other variables.

I want to be able to add the following values into my keychain:

  • Username
  • Password
  • Access Token
  • Refresh Token

Currently I am doing something like this:

KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.myname.myapp" accessGroup:nil];
[keychainItem setObject:usernameField.text forKey:(__bridge id)(kSecAttrAccount)];
[keychainItem setObject:password1Field.text forKey:(__bridge id)(kSecValueData)];

I've tried setting multiple objects as kSecValueData, but it just overwrites the previous set ones.

 [keychainItem setObject:password1Field.text forKey:(__bridge id)(kSecValueData)];
 [keychainItem setObject:access_token forKey:(__bridge id)(kSecValueData)];
 [keychainItem setObject:access_token forKey:(__bridge id)(kSecValueData)];

So I was wondering if anyone knows how I should go about doing this... should I change my initWithIdentifier to be something that like @"com.myname.myapp.accesstoken"? Does anyone else have a better way of doing this? I don't know how the initWithIdentifier or accessGroup really work.

هل كانت مفيدة؟

المحلول

So I created a work around, but I am sure there are better solutions out there.

I created a method that would turn a NSDictionary into a JSON String, and inserted the JSON String into the keychain password, that way you could store multiple values security.

NSDictionary *secretValues = [[NSDictionary alloc] initWithObjectsAndKeys:
                               password, @"password",
                               access_token, @"access_token",
                               refresh_token, @"refresh_token", nil];

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:secretValues
                                                   options:0
                                                     error:&error];


if (! jsonData) {
    NSLog(@"Got an error: %@", error);
} else {
    NSString *jsonString = [[NSString alloc] initWithData:jsonData 
                                                 encoding:NSUTF8StringEncoding];
    [keychainItem setObject:jsonString forKey:(__bridge id)(kSecValueData)];
}

Then if you want to get the password, you could do something like this:

- (NSString *) getPassword{

    KeychainItemWrapper *keychainItem = [[KeychainItemWrapper alloc] initWithIdentifier:@"com.myname.app" accessGroup:nil];

    NSString *password = [[NSString alloc] initWithData:[keychainItem objectForKey:(__bridge id)(kSecValueData)]
                                               encoding:NSUTF8StringEncoding];

    NSDictionary *jsonData =
    [NSJSONSerialization JSONObjectWithData: [password dataUsingEncoding:NSUTF8StringEncoding]
                                    options: NSJSONReadingMutableContainers
                                      error: nil];

    return jsonData[@"password"];
}

I am sure a better way exists, but this was the best I could come up with.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top