IOS APP password saved to keychain

Source: Internet
Author: User

When we open apps, we usually keep some of the more important user information in the app, and many times we do it directly to Nsuserdefaults. The obvious drawback of this approach is that it is not safe enough to use code to encapsulate a class that stores more important information, such as user name account passwords, into Apple's own keychain, so that unless the entire iOS security mechanism is cracked, otherwise your storage will be relatively secure, and there's a good thing When the user deletes the app and downloads the installation again, it can read the data saved to the keychain.

Directly on the code:

. h

////KeychainTool.h//Save the password to the keychain////Created by HO on 16/6/9.//copyright©2016 years HO. All rights reserved.//#import<Foundation/Foundation.h>#import<Security/Security.h>@interfaceYfkeychaintool:nsobject/** * Store string to?? Keychain * * @param svalue corresponding value * @param sKey corresponding key*/+ (void) Savekeychainvalue: (NSString *) svalue key: (NSString *) SKey;/** * from?? Keychain Gets the string * * @param sKey corresponding key * * @return returns the stored value*/+ (NSString *) Readkeychainvalue: (NSString *) SKey;/** * from?? Keychain Delete String * * @param sKey corresponding key*/+ (void) Deletekeychainvalue: (NSString *) SKey;@end

. m

////KEYCHAINTOOL.M//Save the password to the keychain////Created by HO on 16/6/9.//copyright©2016 years HO. All rights reserved.//#import "YFKeychainTool.h"@implementationYfkeychaintool+ (Nsmutabledictionary *) Getkeychainquery: (NSString *) service{return[nsmutabledictionary Dictionarywithobjectsandkeys: (__bridge_transferID) Ksecclassgenericpassword, (__bridge_transferID) Ksecclass,service, (__bridge_transferID) Ksecattrservice,service, (__bridge_transferID) Ksecattraccount, (__bridge_transferID) Ksecattraccessibleafterfirstunlock, (__bridge_transferID) ksecattraccessible, nil];}+ (void) Savekeychainvalue: (NSString *) svalue key: (NSString *) skey{nsmutabledictionary* Keychainquery =[self getkeychainquery:skey];    Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery); [Keychainquery setobject:[nskeyedarchiver Archiveddatawithrootobject:svalue] Forkey: (__bridge_transferID) Ksecvaluedata];    Secitemadd ((__bridge_retained cfdictionaryref) keychainquery, NULL); }+ (NSString *) Readkeychainvalue: (NSString *) skey{NSString*ret =Nil; Nsmutabledictionary*keychainquery =[self getkeychainquery:skey]; [Keychainquery setobject: (ID) kcfbooleantrue Forkey: (__bridge_transferID) Ksecreturndata]; [Keychainquery setobject: (__bridge_transferID) Ksecmatchlimitone Forkey: (__bridge_transferID) Ksecmatchlimit]; Cfdataref KeyData=NULL; if(Secitemcopymatching (__bridge cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = =NOERR) {        @try{ret= (NSString *) [Nskeyedunarchiver unarchiveobjectwithdata: (__bridge NSData *) KeyData]; } @catch(NSException *e) {NSLog (@"unarchive of%@ failed:%@", SKey, E); } @finally {        }    }    if(KeyData) cfrelease (keyData); returnret;}+ (void) Deletekeychainvalue: (NSString *) SKey {nsmutabledictionary*keychainquery =[self getkeychainquery:skey]; Secitemdelete ((__bridge cfdictionaryref) keychainquery);}@end

Viewcontroller Call

////VIEWCONTROLLER.M//Save the password to the keychain////Created by HO on 16/6/9.//copyright©2016 years HO. All rights reserved.//#import "ViewController.h"#import "YFKeychainTool.h"@interfaceViewcontroller () @property (weak, nonatomic) Iboutlet Uitextfield*UserName, @property (weak, nonatomic) Iboutlet Uitextfield*password;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; }-(Ibaction) SAVEBTN: (IDSender {[Yfkeychaintool saveKeychainValue:self.userName.text key:@"UserName"]; [Yfkeychaintool saveKeychainValue:self.password.text key:@"Password"];}-(Ibaction) READEBTN: (ID) Sender {Self.userName.text= [NSString stringWithFormat:@"read to User name:%@", [Yfkeychaintool Readkeychainvalue:@"UserName"]]; Self.password.text= [NSString stringWithFormat:@"read to user password:%@", [Yfkeychaintool Readkeychainvalue:@"Password"]]; }-(Ibaction) DELETEBTN: (IDSender {[Yfkeychaintool deletekeychainvalue:@"UserName"]; [Yfkeychaintool Deletekeychainvalue:@"Password"];}- (void) didreceivememorywarning {[Super didreceivememorywarning]; }@end

:

IOS APP password saved to keychain

Related Article

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.