سؤال

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 ?

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

المحلول

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

نصائح أخرى

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.

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