在使用xmpp框架登录ios应用程序时面临断言失败错误:



我正在尝试实现聊天应用程序。尝试使用授权的用户名和密码登录。已在银行账户中注册。但是,在登录时,我面临以下错误。请指导我,如何解决这个问题。

误差

*** Assertion failure in -[KeychainItemWrapper writeToKeychain], /Users/Downloads/my_ChatInteg/myChatInteg/Classes/HelperClasses/KeychainItemWrapper.m:309
2015-10-10 18:50:28.997 myChatInteg[1057:34611] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Couldn't add the Keychain Item.'

keyChainWrapper.m

- (void)writeToKeychain
{
    NSDictionary *attributes = NULL;
    NSMutableDictionary *updateItem = NULL;
    OSStatus result;
    if (SecItemCopyMatching((CFDictionaryRef)genericPasswordQuery, (CFTypeRef *)&attributes) == noErr)
    {
        // First we need the attributes from the Keychain.
        updateItem = [NSMutableDictionary dictionaryWithDictionary:attributes];
        // Second we need to add the appropriate search key/values.
        [updateItem setObject:[genericPasswordQuery objectForKey:(id)kSecClass] forKey:(id)kSecClass];
        // Lastly, we need to set up the updated attribute list being careful to remove the class.
        NSMutableDictionary *tempCheck = [self dictionaryToSecItemFormat:keychainItemData];
        [tempCheck removeObjectForKey:(id)kSecClass];
#if TARGET_IPHONE_SIMULATOR
        // Remove the access group if running on the iPhone simulator.
        //
        // Apps that are built for the simulator aren't signed, so there's no keychain access group
        // for the simulator to check. This means that all apps can see all keychain items when run
        // on the simulator.
        //
        // If a SecItem contains an access group attribute, SecItemAdd and SecItemUpdate on the
        // simulator will return -25243 (errSecNoAccessForItem).
        //
        // The access group attribute will be included in items returned by SecItemCopyMatching,
        // which is why we need to remove it before updating the item.
        [tempCheck removeObjectForKey:(id)kSecAttrAccessGroup];
#endif
        // An implicit assumption is that you can only update a single item at a time.
        result = SecItemUpdate((CFDictionaryRef)updateItem, (CFDictionaryRef)tempCheck);
        NSAssert( result == noErr, @"Couldn't update the Keychain Item." );
    }
    else
    {
        // No previous item found; add the new one.
        result = SecItemAdd((CFDictionaryRef)[self dictionaryToSecItemFormat:keychainItemData], NULL);
        NSAssert( result == noErr, @"Couldn't add the Keychain Item." );  //APP CRASHES IN THIS LINE
    }
}

保存密钥链中的值

-(BOOL)saveCredentials:(NSDictionary*)dictionary
{
    if([TCUtility saveToKeyChain:[dictionary valueForKey:@"username"] andPassword:[dictionary valueForKey:@"password"]])
    {
        if([XAppDelegate getCredentialsFromKeychain]){
            return YES;
        }else{
            return NO;
        }
    }
    else{
        return NO;
    }
    return 0;
}

我建议你这样做:

KeychainItemWrapper *keyChainManager = [[KeychainItemWrapper alloc]initWithIdentifier:APP_KEYCHAIN accessGroup:nil];
[keyChainManager setObject:(__bridge id)kSecAttrAccessibleAlways forKey:(__bridge id)kSecAttrAccessible];
[keyChainManager setObject:yourObject forKey:key];

请防止添加重复项

相关内容

最新更新