IOS Keychain (keychain) Simple encapsulation

Source: Internet
Author: User

1, keychain is a very useful local storage tool can completely replace nsuserdefault to use and more secure than Nsuserdefault.

2, Keychain relative to nsuserdefault the main point is that Nsuserdefault can only access the project Keychain said that all projects are common to read data across the project.

3, keychain often used to save the account password information keychain in the internal implementation of the actual or use of the database to store here not much to explain

4, use here written dead data can be specific according to the actual situation and Nsuserdefault usage

[Zfykeychain savepassword:@ "Aaa1" forkey:@ "AAA"];
NSLog (@ "%@", [Zfykeychain readpasswordobjectforkey:@ "AAA"]);

[Zfykeychain savepassword:@ "BBB1" forkey:@ "BBB"];
NSLog (@ "%@", [Zfykeychain readpasswordobjectforkey:@ "BBB"]);


[Zfykeychain deletepasswordobjectforkey:@ "AAA"];

NSLog (@ "%@", [Zfykeychain readpasswordobjectforkey:@ "AAA"]);
NSLog (@ "%@", [Zfykeychain readpasswordobjectforkey:@ "BBB"]);

5, set up across the app share data

The first method: through the plist way

1> Create a new file and select the property list file.

2> creates a new child node named Keychain-access-groups

3> Modify node keychain-access-groups type is array

4> Add a child node under the Keychain-access-groups node and fill in your main key name

The second way: Configure directly through the Xcode feature (very simple)

1> Select the Tages>capabilities page in the project configuration

2> Open the keychain sharing switch and set the name of your main key.

Lsf.icetearstest is another app's entitlements file.

Lsf. Bluetooth is the entitlements file for the current project

With each other, you can change the data stored inside the keychain through different apps.


#import <Foundation/Foundation.h>

@interface Zfykeychain:nsobject

/**
* @brief Store password
*
* @param password Password content
*
* @param key
*/
+ (void) Savepassword: (NSString *) password Forkey: (NSString *) key;

/**
* @brief Read the password
*
* @return Password content
*
* @param key
*/
+ (ID) Readpasswordobjectforkey: (NSString *) key;

/**
* @brief Delete password data
*
* @param key
*/
+ (void) Deletepasswordobjectforkey: (NSString *) key;

@end

#import "ZFYKeychain.h"

@implementation Zfykeychain

Static NSString * Const KEY_IN_KEYCHAIN = @ "com. Zfykeychain ";//key used as an indication of access


+ (void) Savepassword: (NSString *) password Forkey: (NSString *) key
{
Nsmutabledictionary *usernamepasswordkvpairs = [Nsmutabledictionary dictionary];
[Usernamepasswordkvpairs Setobject:password Forkey:key];
[Self save:key_in_keychain data:usernamepasswordkvpairs];
}

+ (ID) Readpasswordobjectforkey: (NSString *) key
{
Nsmutabledictionary *usernamepasswordkvpair = (nsmutabledictionary *) [self load:key_in_keychain];
return [Usernamepasswordkvpair Objectforkey:key];
}

+ (void) Deletepasswordobjectforkey: (NSString *) key
{
[Self delete:key];
}

+ (Nsmutabledictionary *) Getkeychainquery: (NSString *) Service {
    return [nsmutabledictionary Dictionarywithobjectsandkeys:
            (__bridge_ Transfer id) Ksecclassgenericpassword, (__bridge_transfer ID) ksecclass,
             service, (__bridge_transfer ID) ksecattrservice,
             service, (__bridge_transfer ID) ksecattraccount,
             (__bridge_transfer ID) ksecattraccessibleafterfirstunlock, (__bridge_ Transfer id) ksecattraccessible,
            Nil];
}

+ (void) Save: (NSString *) service data: (ID) Data {
Get Search Dictionary
Nsmutabledictionary *keychainquery = [self getkeychainquery:service];
Delete old item before add New item
Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery);
Add new object to search dictionary (attention:the data format)
[Keychainquery setobject:[nskeyedarchiver Archiveddatawithrootobject:data] Forkey: (__bridge_transfer ID) Ksecvaluedata];
ADD item to keychain with the search dictionary
Secitemadd ((__bridge_retained cfdictionaryref) keychainquery, NULL);
}

+ (ID) Load: (NSString *) Service {
ID ret = NIL;
Nsmutabledictionary *keychainquery = [self getkeychainquery:service];
Configure the search setting
[Keychainquery setobject: (ID) kcfbooleantrue Forkey: (__bridge_transfer ID) ksecreturndata];
[Keychainquery setobject: (__bridge_transfer ID) ksecmatchlimitone forkey: (__bridge_transfer ID) kSecMatchLimit];
Cfdataref keyData = NULL;
if (secitemcopymatching (__bridge_retained cfdictionaryref) keychainquery, (Cftyperef *) &keydata) = = NOERR) {
@try {
ret = [Nskeyedunarchiver unarchiveobjectwithdata: (__bridge_transfer NSData *) KeyData];
} @catch (NSException *e) {
NSLog (@ "Unarchive of%@ failed:%@", service, E);
} @finally {
}
}
return ret;
}

+ (void) Delete: (NSString *) Service {
Nsmutabledictionary *keychainquery = [self getkeychainquery:service];
Secitemdelete ((__bridge_retained cfdictionaryref) keychainquery);
}

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.