iOS Development--keychain Usage Introduction

Source: Internet
Author: User

The iOS Keychain service provides a secure way to save private information (passwords, serial numbers, certificates, and so on). Each iOS program has a separate keychain store. starting with iOS 3.0, it becomes possible to share keychain across programs .

It is convenient to use Apple's official keychainitemwrapper or sfhfkeychainutils.

Apple already has ready-made classes packaged with Keychain,keychainitemwrapper.h and keychainitemwrapper.m files, which can be found in generickeychain Found in the example.

The following is the use of keychain to implement access to user names and passwords.

The code is as follows:

DMKeyChain.h

#import <Foundation/Foundation.h>
#import <Security/Security.h>
@interface Dmkeychain:nsobject
/**
* Save Data
*
* @param service Key value
* @param data Store value
*/
+ (void) Save: (NSString *) service data: (ID) data;
/**
* Read Data
*
* @param service Key value
*
* @return Stored values
*/
+ (ID) Load: (NSString *) service;
/**
* Delete a data
*
* Key for @param service
*/
+ (void) Delete: (NSString *) service;
@end

Dmkeychain.m

#import "DMKeyChain.h"

@implementation Dmkeychain
+ (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);
}
@end

iOS Development--keychain Usage Introduction

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.