Use Sfhfkeychainutils to save user passwords in iOS

Source: Internet
Author: User

Use Sfhfkeychainutils to save the user password in iOS, you can refer to a friend who needs it.

The user password needs to be saved in the project to enable the automatic login function. So, we studied how to save passwords in iOS:

1. Security method for saving user passwords

As an iphone developer, you need to be responsible for your users ' security. Excuse me, how did you save the user's password? Save directly in the Plist file? Encryption? Aes? Des? Can you ensure that your code is not recompiled to get your encryption key?

It's too much of an apple. I Google a bit, the domestic developers did not notice this problem.

The Apple system has a program called "Keychain" (Keychain), which is not just for you to apply for development certificates. Haha, it can save the password! This is also the best solution that Apple gives to save the password.

In general, we use Nsuserdefaults to store data information, but for some private information, such as passwords, certificates, and so on, we need to use a more secure keychain.

2. Use keychain to store user-sensitive information

The iOS Keychain service provides a secure way to save private information (passwords, serial numbers, certificates, and so on), and each iOS program has a separate keychain store. Compared to nsuserdefaults, file preservation and other general way, keychain save more secure, and keychain saved information will not be deleted because the app is lost, so after reloading the app, keychain data can also be used. Starting with iOS 3.0, it becomes possible to share keychain across programs.

How do I use keychain in my application? We need to import security.framework, Keychain's operation interface is declared in the header file SecItem.h. Using the SecItem.h method to manipulate the keychain directly, the code that needs to be written is more complex. With the native Security.framework, the keychain can be accessed, read and written, but only on the real machine, and the simulator will go wrong. To alleviate the development of our programmers, we can use some of the tools that have been packaged on GitHub, so that you can access the keychain on the real machine as well as the emulator. For example: Sfhfkeychainutils and Keychainitemwrapper, here is mainly about the use of sfhfkeychainutils.

3. Using Sfhfkeychainutils to save the user password Sfhfkeychainutils is another third-party class library that encapsulates keychain simple operations, with a simpler point than keychainitemwrapper. Sfhfkeychainutils only provides three ways to get, save, and delete:
    + (NSString *) Getpasswordforusername: (NSString *) Username andservicename: (nsstring *) ServiceName Error: (Nserror *) Error;    + (BOOL) Storeusername: (NSString *) Username Andpassword: (nsstring *) password Forservicename: (NSString *) serviceName Updateexisting: (BOOL) updateexisting error: (NSERROR * *) error;    + (BOOL) Deleteitemforusername: (NSString *) Username andservicename: (nsstring *) ServiceName Error: (NSERROR *) error;
4. Example Sfhfkeychainutils:https://github.com/ldandersen/scifihifi-iphone/tree/master/security. New project, add downloaded SFHFKeychainUtils.h and SFHFKEYCHAINUTILS.M files to the project, because the downloaded sfhfkeychainutils does not support arc, so in the Targets->build The SFHFKEYCHAINUTILS.M in Phases->complie Resources adds the-fno-objc-arc compiler Flags. Finally, add the Security.framework framework. The code for VIEWCONTROLLER.H is:
#import <UIKit/UIKit.h> @interface Viewcontroller:uiviewcontroller@property (nonatomic, strong) NSString * UserName; @property (nonatomic, strong) NSString *userpassword; @property (nonatomic, strong) NSString *servicename;  Service (group)-(Ibaction) Savepassword: (ID) sender;-(ibaction) GetPassword: (ID) sender;-(ibaction) of this article keychains Deletepassword: (ID) sender; @end
The code for VIEWCONTROLLER.M is:
#import "ViewController.h" #import "SFHFKeychainUtils.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload{[super Viewdidload];}    -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.}    -(Ibaction) Savepassword: (ID) Sender {self.username = @ "[email protected]";    Self.userpassword = @ "123456";  Self.servicename = @ "Com.china.TestKeyChain";    This article keychains belongs to the Service (group) Nserror *error;                                   BOOL saved = [Sfhfkeychainutils storeUsername:self.userName AndPassword:self.userPassword        ForServiceName:self.serviceName Updateexisting:yes error:&error]; if (!saved) {NSLog (@ "?)    Keychain Error saving Password:%@ ", error); }else{NSLog (@ "? Keychain Save password successfully!    ");    }}-(ibaction) GetPassword: (ID) Sender {self.username = @ "[email protected]";    Self.userpassword = @ "123456";  Self.servicename = @ "Com.china.TestKeyChain"; This article keychainsThe Service (group) of the genus Nserror *error; NSString * string = [sfhfkeychainutils getPasswordForUsername:self.userName andServiceName:self.serviceName Error:        &error];    if (!string) {Self.userpassword = string;    } if (Error | |!string) {NSLOG (@ ") error getting password from keychain:%@", error); } else{NSLog (@ "? Get the password from keychain!    Password is%@ ", Self.userpassword);    }}-(ibaction) Deletepassword: (ID) Sender {self.username = @ "[email protected]";    Self.userpassword = @ "123456";  Self.servicename = @ "Com.china.TestKeyChain";    This article keychains belongs to the Service (group) Nserror *error;    BOOL isDeleted; isDeleted = [Sfhfkeychainutils deleteItemForUsername:self.userName andServiceName:self.serviceName Error:&error        ]; if (!isdeleted) {NSLog (@ "?) Keychain Delete

Use Sfhfkeychainutils to save user passwords in iOS

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.