How can we save NSArray using KeychainItemWrapper class(ARC)

孤人 提交于 2019-12-11 14:29:30

问题


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.


回答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"];


来源:https://stackoverflow.com/questions/19109739/how-can-we-save-nsarray-using-keychainitemwrapper-classarc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!