0

I want to save NSArray using KeychainItemWrapper class

I have come to know that we can store the NSDictionary after reading This Question

But its not working

This is what I have done so far

NSArray *myArray = [[NSArray alloc]initWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4", nil];
NSDictionary *myDic  = [[NSDictionary alloc]initWithObjectsAndKeys:myArray, @"arrayKey", nil];

NSString *error;
NSData *dictionaryRep = [NSPropertyListSerialization dataFromPropertyList:myDic format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];

KeychainItemWrapper *keychain =
[[KeychainItemWrapper alloc] initWithIdentifier:@"MyIdentifier" accessGroup:nil];

[keychain setObject:dictionaryRep forKey:(__bridge id)kSecValueData];

But it is crashing at last line where we are setting object.

Erro Logs:

2013-10-01 12:16:47.590 stackoverflowtry[3883:a0b] -[__NSCFData dataUsingEncoding:]: unrecognized selector sent to instance 0xa1686a0
2013-10-01 12:16:47.593 stackoverflowtry[3883:a0b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFData dataUsingEncoding:]: unrecognized selector sent to instance 0xa1686a0'

Need Help.

Community
  • 1
  • 1
Prashant Nikam
  • 2,253
  • 4
  • 17
  • 29
  • Please let me know how to save either NSDictionary or NSArray, both will work for me.. – Prashant Nikam Oct 01 '13 at 06:52
  • Why you use "(__bridge id)kSecValueData" as key for storing in dictionary?, did you log this value to check value? – josh Oct 01 '13 at 07:36
  • @josh : (__bridge id)kSecValueData is a predefined key in KeychainItemWrapper.m file...I dont thing we are allowed to use user defines keys....and this code works fine if I just pass NSString instead of dictionaryRep. – Prashant Nikam Oct 01 '13 at 07:55

1 Answers1

1

i have idea. you can try this. you must #import "SBJsonWriter.h"

    NSArray *myArray = [[NSArray alloc]initWithObjects:@"Hello1",@"Hello2",@"Hello3",@"Hello4", nil];
NSDictionary *myDic  = [[NSDictionary alloc]initWithObjectsAndKeys:myArray, @"arrayKey", nil];


KeychainItemWrapper *keychain =
[[KeychainItemWrapper alloc] initWithIdentifier:@"MyIdentifier" accessGroup:nil];

    SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
    NSString *jsonString = @"";

        jsonString = [jsonWriter stringWithObject:myDic];

    [keychain setObject:jsonString forKey:(__bridge id)(kSecAttrDescription)];

If you want to get it

NSString *JsonString= [keychain objectForKey:(__bridge id)(kSecAttrDescription)];
NSArray *myArray=  [[JsonString JSONValue] objectForKey:@"arrayKey"];
Luna
  • 113
  • 1
  • 6
  • NSlog(@"JsonString = ",jsonString); jsonString return = ? you create KeychainItemWrapper *keychain = [[KeychainItemWrapper alloc] initWithIdentifier:@"demo" accessGroup:nil]; ? – Luna Oct 04 '13 at 07:20