IOS Keychain Storage

Source: Internet
Author: User

Simple translation, keychain can store passwords, certificates and so on, as to what benefits, oh da, their own Baidu go, summed up a bit is more secure, and take more convenient bar, see the app scene bar.

This code, divided into two paragraphs, a section for saving code

 1-(BOOL) SaveData: (NSString *) Str 2 {3 Osstatus status = NOERR; 4 nsmutabledictionary *query = [[Nsmutabledicti] Onary alloc] init]; 5 [Query SetObject: (ID) Ksecclassgenericpassword Forkey: (ID) ksecclass]; 6//Query one 7 [query setobject: (ID) ksecmatchlimitone Forkey: (ID) ksecmatchlimit]; 8 [Query setobject:@ "Password" Forkey: (ID) ksecattrservice]; 9 Cfmutabledictionaryref outdicref = nil;10 status = Secitemcopymatching ((cfdictionaryref) query, (CFTYPEREF *) &amp , Outdicref), if (Status==noerr) {NSLog (@ "###################### #query success"), [query re]         Moveobjectforkey: (ID) ksecmatchlimit];15 NSLog (@ "=============code%d", Secitemdelete ((cfdictionaryref) query)); 16 Nsmutabledictionary *DT = [[Nsmutabledictionary alloc] init];17 [DT setobject:@ "label" Forkey: (ID) ksecattr label];18 [dt setobject:@ "Password" Forkey: (ID) ksecattrservice];19 [dt Setobject:[str Datausingencoding:ns Utf8stringencodiNG] Forkey: (ID) ksecvaluedata];20 [dt setobject: (ID) Ksecclassgenericpassword Forkey: (ID) ksecclass];21 22 [DT setobject:@ "Item label" Forkey: (__bridge ID) ksecattrlabel];23 [dt setobject:@ "Item Descriptio N "Forkey: (__bridge ID) ksecattrdescription];25 [dt setobject:@" Account "Forkey: (__bridge ID) KSECATTRACC ount];27 [DT setobject:@ "Service" Forkey: (__bridge ID) ksecattrservice];29 [dt Setobj ect:@ "Your comment here." Forkey: (__bridge id) ksecattrcomment];31 status = Secitemadd ((cfdictionaryref) DT, NULL); 3 2 if (status==noerr) {NSLog (@ "###################### #save success");             yes;36}37 return yes;38}39 else40 if (status==errsecitemnotfound) 41 {42 NSLog (@ "#################### #not found"),//store44 nsmutabledictionary *dt = [nsmutabledict  Ionary Alloc] Init];45           [DT setobject:@ "label" Forkey: (ID) ksecattrlabel];46 [dt setobject:@ "Password" Forkey: (ID) ksecattrse             rvice];47 [DT setobject:[str datausingencoding:nsutf8stringencoding] Forkey: (ID) ksecvaluedata];48  [DT SetObject: (ID) Ksecclassgenericpassword Forkey: (ID) ksecclass];49 [dt setobject:@ "Item label" Forkey: (__bridge ID) ksecattrlabel];51 [dt setobject:@ "Item description" Forkey: (__bridge ID) kS             ecattrdescription];53 [dt setobject:@ "Account" Forkey: (__bridge ID) ksecattraccount];55 [DT setobject:@ "Service" Forkey: (__bridge ID) ksecattrservice];57 [dt Setobje ct:@ "Your comment here." Forkey: (__bridge id) ksecattrcomment];59 status = Secitemadd ((cfdictionaryref) DT, NULL                 ), if (Status==noerr) NSLog (@ "###################### #save Success"), 63 Return yes;64}65 if (outdicref) cfrelease (outdicref); return no;68}69 else70 {NSLog (@ "####################### #error query%d", status);}73 return no;74}

Code a little messy, you will be next, anxious to go to bed ~

Read the code as follows:

 1-(NSString *) GetData 2 {3 Osstatus status = NOERR; 4 nsmutabledictionary *query = [[Nsmutabledictionary alloc ] init]; 5 [Query SetObject: (ID) Ksecclassgenericpassword Forkey: (ID) ksecclass]; 6//Query one 7 [query setobject: (ID) ksecmatchlimitone Forkey: (ID) ksecmatchlimit]; 8 [Query setobject:@ "Service" Forkey: (ID) ksecattrservice]; 9///If you want to get the relevant properties, you must set it here.  In this place, I've been doing this for half a day. [Query setobject: (ID) kcfbooleantrue Forkey: (ID) ksecreturnattributes];11 cfdictionaryref attributes =     NIL;12////Take attribute to the status = Secitemcopymatching ((__bridge cfdictionaryref) query, (CFTYPEREF *) &attributes); 14 if (status==noerr) {NSLog (@ "--------%@", attributes);}18 [Query Removeobjectforkey: (ID) KSECR eturnattributes];19 cfdataref outdata;20////value, where the actual password is stored, here to set the [query SetObject: (ID) kcfbooleantrue Forkey: (i   D) ksecreturndata];22 status = Secitemcopymatching ((cfdictionaryref) query, (CFTYPEREF *) &outdata); 23//Parse back 24  NSString *str = [[NSString alloc] Initwithdata: (__bridge nsdata *) Outdata encoding:nsutf8stringencoding];25 return s TR;26}

Under test:

  

Take to property:

  

Take password

  

IOS Keychain Storage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.