Ios--keychina Simple to use

Source: Internet
Author: User

Today to understand Keychina, previously saved user information is placed in Nsuserdefaults, this is not safe, now write I will put in Keychina, because he stored the information is still relatively safe, Keychain information is present in the sandbox of each app (app), so the information stored in keychain will not be lost due to the deletion of the app and remains valid after the user re-installs the app. (that is, to store some sensitive information);

Then let's simply integrate it, save the user's account number and password, there are other uses, there is no introduction:

Before the integration, Keychina now does not support arc, so we need to manually configure the project, in the code will be reflected,

First import the framework of the system:

Then we create a new class: Keychina;

. M:

#import<Foundation/Foundation.h>#import<Security/Security.h>@interfaceKeychina:nsobject/** * Use Keychaina to save user name and password*/+(void) Save: (NSString *) Service data: (ID) data;/** * Remove user name and password from Keychina*/+(ID) Load: (NSString *) service;/** * Remove user name and password from Keychina*/+ (void) Delete: (NSString *) service;@end

. h Implementations:

#import "KeyChina.h"@implementationKeychina/** * This class needs to work in MRC mode, if the project is arc, follow the steps below * Select the project->targets-> the corresponding target and select "Build phases" on the right and find "Compile Sources" down The Then add it after the corresponding file:-fno-objc-arc parameter*/+ (Nsmutabledictionary *) Getkeychinaquery: (NSString *) service{return[Nsmutabledictionary Dictionarywithobjectsandkeys: (ID) Ksecclassgenericpassword, (ID) Ksecclass, service, (ID) Ksecattrservice, service, (ID) Ksecattraccount, (ID) Ksecattraccessibleafterfirstunlock, (ID) ksecattraccessible, nil]; }#pragmaMark--Write + (void) Save: (NSString *) Service data: (ID) data{//Get Search DictionaryNsmutabledictionary *keychinaquery =[self getkeychinaquery:service]; //Delete old item before add New itemSecitemdelete ((cfdictionaryref) keychinaquery); //Add New object to search dictionary (attention:the data format)[Keychinaquery setobject:[nskeyedarchiver Archiveddatawithrootobject:data] Forkey: (ID) Ksecvaluedata]; //ADD item to keychain with the search dictionarySecitemadd ((cfdictionaryref) keychinaquery, NULL);}#pragmaMark-Read + (ID) Load: (NSString *) service{IDRET =Nil; Nsmutabledictionary*keychinaquery =[self getkeychinaquery:service]; //Configure the search setting//Since in our simple case we is expecting only a single attribute to be returned (the password) we can set the Attribu Te Ksecreturndata to Kcfbooleantrue[Keychinaquery SetObject: (ID) Kcfbooleantrue Forkey: (ID) Ksecreturndata]; [Keychinaquery setobject: (ID) Ksecmatchlimitone Forkey: (ID) Ksecmatchlimit]; Cfdataref KeyData=NULL; if(Secitemcopymatching (Cfdictionaryref) keychinaquery, (Cftyperef *) &keydata) = =NOERR) {        @try{ret= [Nskeyedunarchiver unarchiveobjectwithdata: (NSData *) KeyData]; } @catch(NSException *e) {NSLog (@"unarchive of%@ failed:%@", service, E); } @finally {        }    }    if(KeyData) cfrelease (keyData); returnret; }#pragmaMark--Delete + (void) Delete: (NSString *) service{nsmutabledictionary*keychinaquery =[self getkeychinaquery:service]; Secitemdelete ((cfdictionaryref) keychinaquery);}@end

Call where needed:

#import "ViewController.h"#import "KeyChina.h"@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.NSString *ConstKey_username_password =@"Com.company.app.usernamepassword"; NSString*ConstKey_username =@"Com.company.app.username"; NSString*ConstKey_password =@"Com.company.app.password"; Nsmutabledictionary*usernamepasswordkvpairs =[Nsmutabledictionary dictionary]; [Usernamepasswordkvpairs setobject:@"UserName"Forkey:key_username]; [Usernamepasswordkvpairs setobject:@"Password"Forkey:key_password]; //A. Write user name and password to keychain[Keychina Save:key_username_password data:usernamepasswordkvpairs]; //B. Read the user name and password from the keychainNsmutabledictionary *readusernamepassword = (Nsmutabledictionary *) [Keychina Load:key_username_password]; NSString*username =[Readusernamepassword Objectforkey:key_username]; NSString*password =[Readusernamepassword Objectforkey:key_password]; NSLog (@"username =%@", UserName); NSLog (@"Password =%@", password); //C. Remove the user name and password from the keychain[Keychina Delete:key_username_password];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.    }@end

OK,!!!

Ios--keychina Simple to use

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.