0

i'm use keychain in my app with keychainitemwrapper. So, i did update 'kSecAttrAccessible' use setObject:(id) method.
But errors occur. Like this,

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't update the Keychain Item.'

and this is my source

KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"UserData" accessGroup:@"49YVVGB32W.com.covimdm.daelim.pushData"];
NSString *PushData = [NSString stringWithFormat:@"%@::%@::%@", sLoginId, [[[PushManager defaultManager] info] pushServiceID], [[[PushManager defaultManager] info] host]];
[wrapper setObject:PushData forKey:(id)kSecValueData];
[wrapper setObject:(id)kSecAttrAccessibleAfterFirstUnlock forKey:(id)kSecAttrAccessible];

The part where the error occurs is the fourth line code.

[wrapper setObject:(id)kSecAttrAccessibleAfterFirstUnlock forKey:(id)kSecAttrAccessible];

i don't know why error occurs on and over again.

OreonHard
  • 3
  • 3

1 Answers1

0

EDIT

Ok, let us try again. The wrapper library you use is specifically for a single object it seems, so it seems to crash when you add the keychain attributes. However, it seems to work if you do it first. So first load the configuration and then the item data as below and it should work.

    KeychainItemWrapper *wrapper = [[KeychainItemWrapper alloc] initWithIdentifier:@"UserData"
                                       accessGroup:@"49YVVGB32W.com.covimdm.daelim.pushData"];

    NSString * pushString = @"Some string";
    NSData   * pushData   = [pushString dataUsingEncoding:NSUTF8StringEncoding];

    [wrapper setObject:kSecAttrAccessibleAfterFirstUnlock
            forKey:kSecAttrAccessible]; // Do configuration first
    [wrapper setObject:pushString // Must be string!?
            forKey:kSecValueData];

The code above is working on this side, albeit with KeychainItemWrapper from here. From the source it seems the item is written each time you set an object, so therefore I think you need the configuration first.

Also, it did NOT work if I stored data as I suggested earlier. It seems you have to store the string. The code above worked fine this side, hope it does the same for you.

skaak
  • 2,988
  • 1
  • 8
  • 16
  • Thanks your answer. but my problem occur while changing kSecAttrAccessible value to kSecAttrAccessibleAfterFirstUnlock. I've been looking at that problem today. And OSStatus return -50 error code. Is it possible to answer this problem? – OreonHard Nov 02 '20 at 07:55
  • Sorry, I misunderstood you - looking at this, going in circles a bit ... will update later – skaak Nov 02 '20 at 08:07
  • Oh... I check your comment just before. I'll waiting your answer ;) – OreonHard Nov 02 '20 at 08:13
  • I looked at it quickly - I think you need to do as I suggest anyhow as you need data for the ```kSecValueData``` key ... the rest look fine so I don't think that is it. Also, that -50 code means something is illegal when storing so that is why I say this. – skaak Nov 02 '20 at 08:19
  • Hmm okay, i'll just do what you suggest me. – OreonHard Nov 02 '20 at 08:35
  • Unfortunately, error isn't resolved. Thank you for your answer, though. – OreonHard Nov 02 '20 at 08:44
  • I think it should work now - please check again. Also I mention the ```KeychainItemWrapper``` in the answer - check that you are using the same one. – skaak Nov 02 '20 at 09:47
  • Im sorry for answering late. I clear this problem and I got a hint from what you said. My keychain has already stored value about kSecAttrAccessible. So i reset my keychain when starting app. And saved data and kSecAttrAccessible at empty Keychain its working! I guess if the permissions are lower than the kSecAttrAccessible you are trying to update, it doesn't seem to be updated. – OreonHard Nov 17 '20 at 05:28