0

我正在尝试使用 KeychainItemWrapper 类将 NSMutableDictionary 保存在 iOS 钥匙串中。但我无法保存它。我收到错误

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法添加钥匙串项。”

这是我要保存的数据

    {
    country = USA;
    id = 3;
    name = "Test User";
    photo = "http://www.mydomain.com/images/user1.jpg";
    result = true;
    "country" = 1;
}

这是我的代码

// Call to save 
[self storeLoggedInUserInfoInKeychainWithDictionary:dict];


        -(void)storeLoggedInUserInfoInKeychainWithDictionary:(NSMutableDictionary*)dict
    {
        // Save Login Credentials
        KeychainItemWrapper* loginUserkeychain = [[KeychainItemWrapper alloc] initWithIdentifier:LOGIN_USER_SERVICE accessGroup:nil];
        NSString *error;
        [loginUserkeychain setObject:(__bridge id)(kSecAttrAccessibleWhenUnlocked) forKey:(__bridge id)(kSecAttrAccessible)];
        NSData *dictionaryRep = [NSPropertyListSerialization dataFromPropertyList:dict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
        [loginUserkeychain setObject:dictionaryRep forKey:(__bridge id)(kSecValueData)];
    }

    -(NSMutableDictionary*)fetchLoggedInUserInfoFromKeychain
    {
        KeychainItemWrapper* loginUserkeychain = [[KeychainItemWrapper alloc] initWithIdentifier:LOGIN_USER_SERVICE accessGroup:nil];
        NSString *error;
        //When the NSData object object is retrieved from the Keychain, you convert it back to NSDictionary type
        NSData *dictionaryRep = [loginUserkeychain objectForKey:(__bridge id)(kSecValueData)];
        NSDictionary *dictionary = [NSPropertyListSerialization propertyListFromData:dictionaryRep mutabilityOption:NSPropertyListImmutable format:nil errorDescription:&error];
        if (error) {
            NSLog(@"%@", error);
        }
        return [NSMutableDictionary dictionaryWithDictionary:dictionary];
    }

    -(void)resetLoggedInUserInfoFromKeychain
    {
        KeychainItemWrapper* loginUserkeychain = [[KeychainItemWrapper alloc] initWithIdentifier:LOGIN_USER_SERVICE accessGroup:nil];
        [loginUserkeychain resetKeychainItem];
    }

谁能告诉我上面的代码有什么问题?提前致谢。

4

1 回答 1

1

经过几次尝试和使用下面的代码研究后,我能够将数据保存在钥匙串中。有兴趣的可以看看下面的代码

    -(void)storeLoggedInUserInfoInKeychainWithDictionary:(NSMutableDictionary*)dict
{
    // Create encoded data
    NSData *encodedData= [NSKeyedArchiver archivedDataWithRootObject:dict];

    // Create encoded string from data
    NSString *encodedString= [encodedData base64EncodedString];

    // Save Login Credentials
    KeychainItemWrapper* tranxkeychain = [[KeychainItemWrapper alloc] initWithIdentifier:LOGIN_USER_KEYCHAIN accessGroup:nil];
    [tranxkeychain setObject:(__bridge id)(kSecAttrAccessibleWhenUnlocked) forKey:(__bridge id)(kSecAttrAccessible)];
    [tranxkeychain setObject:LOGIN_USER_SERVICE forKey: (__bridge id)kSecAttrService];
    [tranxkeychain setObject:LOGIN_USER_INFO forKey:(__bridge id)(kSecAttrAccount)];
    [tranxkeychain setObject:encodedString forKey:(__bridge id)(kSecValueData)];
}

-(NSDictionary*)fetchLoggedInUserInfoFromKeychain
{
    KeychainItemWrapper* tranxkeychain = [[KeychainItemWrapper alloc] initWithIdentifier:LOGIN_USER_KEYCHAIN accessGroup:nil];
    [tranxkeychain setObject:(__bridge id)(kSecAttrAccessibleWhenUnlocked) forKey:(__bridge id)(kSecAttrAccessible)];
    [tranxkeychain setObject:LOGIN_USER_SERVICE forKey: (__bridge id)kSecAttrService];

    // Get decoded string
    NSString *decodedString=[tranxkeychain objectForKey:(__bridge id)(kSecValueData)];

    // Get decoded data
    NSData *decodedData= [NSData dataFromBase64String:decodedString];
    NSDictionary *dict =[NSKeyedUnarchiver unarchiveObjectWithData:decodedData];
    return dict;
}

-(void)resetLoggedInUserInfoFromKeychain
{
    KeychainItemWrapper* tranxkeychain = [[KeychainItemWrapper alloc] initWithIdentifier:LOGIN_USER_KEYCHAIN accessGroup:nil];
    [tranxkeychain resetKeychainItem];
}
于 2013-10-02T10:40:18.583 回答