2

I recently installed iOS 7.1 simulator and the new Xcode 5.1. My App worked in iOS 7 fine. I'm using the KeychainItemWrapper class from Apple. After the update it crashed with the following message:

*** Assertion failure in -[KeychainItemWrapper writeToKeychain]

Specifically at line 299:

NSAssert( result == noErr, @"Couldn't update the Keychain Item." );

Error is hear -25300 (errSecItemNotFound)

I have specified the Keychain Access Group in my entitlements file. This error occurred only in the iOS 7.1 Simulator and not on a real iPhone or the 7.0 Simulator.

Does anyone know what changed with Keychain in 7.1 ?

flashspys
  • 844
  • 9
  • 20

2 Answers2

5

Well KeychainItemWrapper is a old implementation full of bugs. I would recommend using another wrapper, or write your own.

That being said, I used to have many error but not this one. Basically what happens is while saving it checks that your item is already in the keychain, in order to add it or just update it. Here that check returns true even though the item is quite different, so it cannot update because SecItemUpdate believes it does not exist.

What you should do is reset your keychain, you have two options for this :

  • On the simulator Menu Simulator->Reset content and settings

  • Run this snippet somewhere in your code :

Based on Vegard answer here Reset An iPhone App's Keychain

-(void)resetKeychain {
     [self deleteAllKeysForSecClass:kSecClassGenericPassword];
     [self deleteAllKeysForSecClass:kSecClassInternetPassword];
     [self deleteAllKeysForSecClass:kSecClassCertificate];
     [self deleteAllKeysForSecClass:kSecClassKey];
     [self deleteAllKeysForSecClass:kSecClassIdentity];
}

-(void)deleteAllKeysForSecClass:(CFTypeRef)secClass {
     NSMutableDictionary* dict = [NSMutableDictionary dictionary];
    [dict setObject:(__bridge id)secClass forKey:(__bridge id)kSecClass];
    OSStatus result = SecItemDelete((__bridge CFDictionaryRef) dict);
     NSAssert(result == noErr || result == errSecItemNotFound, @"Error deleting keychain data (%ld)", result);
}
Community
  • 1
  • 1
streem
  • 9,044
  • 5
  • 30
  • 41
  • You were very close. The error code is 25300. I edited my Question. Do you know a recommendable Keychain Wrapper ? – flashspys Mar 13 '14 at 15:44
  • [SSKeychain](https://github.com/soffes/sskeychain) is popular, and simple to use. – Abizern Mar 13 '14 at 15:46
  • Thank you very much. I think it's important to know that reset the Location & Privacy did **not** worked. Only after delete all keys with the code snippet once, I'm now able to run my app. – flashspys Mar 13 '14 at 17:12
  • I've indeed updated my answer, it is by reseting content and settings available in the menu of the simulator and not in the simulator itself. – streem Mar 13 '14 at 18:09
2

Keychain continues to work under iOS 7.1. Your issue is related to Simulator itself. Simulator does not allow to store certain keys as far as I know, and this is consistent between iOS 7.0 and iOS 7.1.

If you run your app on a real device, the crash will disappear.

Martin Koles
  • 5,177
  • 8
  • 39
  • 59
  • that's true. But _why_ does it crash on the 7.1 simulator? It would be nice to run my app in the simulator, too. – flashspys Mar 13 '14 at 15:46