0

KeychainItemWrapper クラスを使用して iOS キーチェーンに NSMutableDictionary を保存しようとしています。しかし、私はそれを保存することができません。エラーが発生しています

キャッチされない例外 '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 に答える