The understanding of keychain can be referred to http://my.oschina.net/w11h22j33/blog/206713
The article uses keychain to save the user name and password, the user name is placed in the attribute, the password is placed in the password entry, and I personally feel that there is a problem, in order to respect the use of their own code, but also to help read the other readers of the text better understanding, I amended the code as follows:
Storage/Add-(Ibaction) saveclicked: (ID) sender{if (_nametf.text.length > 0 && _psdtf.text.length > 0) { nsmutabledictionary* dic = [Nsmutabledictionary dictionary]; [DiC setobject: (__bridge_transfer ID) ksecclassgenericpassword forkey: (__bridge_transfer ID) ksecclass]; [DiC setobject:_nametf.text Forkey: (__bridge_transfer ID) ksecattraccount]; [dic Setobject:[_psdtf.text datausingencoding:nsutf8stringencoding] Forkey: (__bridge_transfer ID) kSecValueData]; Osstatus s = Secitemadd ((__bridge_retained cfdictionaryref) dic, NULL); NSLog (@ "%d", s); }}//Get/Query-(ibaction) showclicked: (ID) sender{if (_nametf.text.length > 0) {nsdictionary* query = [nsdictionary Dictionarywithobjectsandkeys: (__bridge_transfer ID) Ksecclassgenericpassword, (__bridge_tra Nsfer ID) Ksecclass, _nametf.text, (__bridge_transfer ID) ksecattraccount, KcfbOoleantrue, Ksecreturnattributes,nil]; Cftyperef result = nil; Osstatus s = secitemcopymatching ((__bridge_retained cfdictionaryref) query, &result); NSLog (@ "%d", s); NSLog (@ "%@", result); if (s = = NOERR) {//through the user to determine whether the item item exists nsmutabledictionary* dic = [nsmutabledictionary dictionarywithdictionary: (__b Ridge_transfer ID) result]; [DiC setobject: (__bridge_transfer ID) kcfbooleantrue forkey: (__bridge_transfer ID) ksecreturndata]; [DiC setobject:[query Objectforkey: (__bridge_transfer ID) ksecclass] forkey: (__bridge_transfer ID) kSecClass]; Cfdataref dataref = NULL; if (secitemcopymatching (__bridge_retained cfdictionaryref) dic, (cftyperef*) &dataref) = = NOERR) { NSData *data = (__bridge_transfer id) dataref; NSLog (@ "%ld", data.length); NSLog (@ "%@", [[NSString alloc] initwithdata:data enCoding:nsutf8stringencoding]); }}}else{nsdictionary* query = [nsdictionary Dictionarywithobjectsandkeys: (__bridge_transfer ID) Ksecclassgenericpassword, (__bridge_transfer ID) Ksecclass, (_ _bridge_transfer ID) Ksecmatchlimitall, (__bridge_transfer ID) ksecmatchlimit, (__bridge_trans Fer id) kcfbooleantrue, (__bridge_transfer ID) Ksecreturnattributes,nil]; Cfarrayref result = NULL; Osstatus s = secitemcopymatching ((__bridge_retained cfdictionaryref) query, (cftyperef*) &result); Nsarray *array = (__bridge_transfer nsarray*) result;//(can traverse all keychain items) NSLog (@ "%d", s); NSLog (@ "%@", result); }}//Delete-(ibaction) delclicked: (ID) sender{if (_nametf.text.length > 0) {nsdictionary* query = [nsdic Tionary Dictionarywithobjectsandkeys: (__bridge_transfer ID) ksecclassgenericpassword,ksecclass, _nametf.text,ksecattraccount,nil]; Osstatus status = Secitemdelete ((__bridge_retained cfdictionaryref) query); NSLog (@ "%d", status); }}//-(Ibaction) modifyclicked: (ID) sender{if (_nametf.text.length > 0 && _psdtf.text.length > 0) { nsdictionary* query = [nsdictionary Dictionarywithobjectsandkeys: (__bridge_transfer ID) Ksecclassgenericpassword, (__bridge_transfer ID) ksecclass, _nametf.text, (__bridge_transfer ID) Ksecattraccount, (__bridge_transfer ID) kcfbooleantrue, (__bridge_transfer ID) ksecreturnat Tributes,nil]; Cftyperef result = nil; if (secitemcopymatching (__bridge_retained cfdictionaryref) query, &result) = = NOERR)//exist only modify {//settings need more New attribute list, be sure to delete ksecclass, because can not update should not field, otherwise will error nsmutabledictionary* update = [[Nsmutabledictionary alloc]init]; [Update setobject:[_psdtf.tExt datausingencoding:nsutf8stringencoding] Forkey: (__bridge_transfer ID) ksecvaluedata]; [Update Removeobjectforkey: (__bridge_transfer ID) ksecclass]; Set the find condition, which is to find the keychain item according to this condition nsmutabledictionary* updateItem = [[Nsmutabledictionary alloc]init]; [UpdateItem setobject:[query Objectforkey: (__bridge_transfer ID) ksecclass] forkey: (__bridge_transfer ID) kSecClass]; [UpdateItem setobject:_nametf.text Forkey: (__bridge_transfer ID) ksecattraccount]; Parameter one means to look for virtual, parameter two means the updated value osstatus status = Secitemupdate ((__bridge_retained cfdictionaryref) UpdateItem, (__bridge _retained cfdictionaryref) update); NSLog (@ "%d", status); } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
IOS Keychain Understanding and code examples