本地保存用户名及密码

2016-08-17  本文已影响109人  晓龙歌

方法一:

今天在做联网版本的离线操作时,需要使用本地验证密码和用户名,然后就发现了这个keychain方法。可以很快捷的存储和读取你要的东西。下面我们来一起简单的应用它一下吧!

学习地址:http://www.lvtao.net/ios/ios-keychain.html

ios的keychain服务提供了一种安全的保存私密信息(密码,序列号,证书等)的方式。每个ios程序都有一个独立的keychain存储。从ios 3.0开始,跨程序分享keychain变得可行。

下面就使用keychain来实现存取用户名和密码。

苹果已经有现成的类封装好了keychain,KeychainItemWrapper.h和KeychainItemWrapper.m文件,可以在GenericKeychain实例里找到。

但是这里我只需要存取用户名和密码,就不用苹果提供的类了,自己写个简单的类来实现就好。

代码如下:

CHKeychain.h

#import

#import

@interface CHKeychain : NSObject

+ (void)save:(NSString *)service data:(id)data;

+ (id)load:(NSString *)service;

+ (void)delete:(NSString *)service;

@end

CHKeychain.m

#import "CHKeychain.h"

@implementation CHKeychain

+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service {

return [NSMutableDictionary dictionaryWithObjectsAndKeys:

(id)kSecClassGenericPassword,(id)kSecClass,

service, (id)kSecAttrService,

service, (id)kSecAttrAccount,

(id)kSecAttrAccessibleAfterFirstUnlock,(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((CFDictionaryRef)keychainQuery);

//Add new object to search dictionary(Attention:the data format)

[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];

//Add item to keychain with the search dictionary

SecItemAdd((CFDictionaryRef)keychainQuery, NULL);

}

+ (id)load:(NSString *)service {

id ret = nil;

NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];

//Configure the search setting

//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue

[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];

[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];

CFDataRef keyData = NULL;

if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {

@try {

ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData];

} @catch (NSException *e) {

NSLog(@"Unarchive of %@ failed: %@", service, e);

} @finally {

}

}

if (keyData)

CFRelease(keyData);

return ret;

}

+ (void)delete:(NSString *)service {

NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];

SecItemDelete((CFDictionaryRef)keychainQuery);

}

@end

首先需要定义几个字符串用来做key:

NSString * const KEY_USERNAME_PASSWORD = @"com.company.app.usernamepassword";

NSString * const KEY_USERNAME = @"com.company.app.username";

NSString * const KEY_PASSWORD = @"com.company.app.password";

把用户名和密码存入keychain:

NSMutableDictionary *usernamepasswordKVPairs = [NSMutableDictionary dictionary];

[usernamepasswordKVPairs setObject:txtfldUsername.text forKey:KEY_USERNAME];

[usernamepasswordKVPairs setObject:txtfldPassword.text forKey:KEY_PASSWORD];

[CHKeychain save:KEY_USERNAME_PASSWORD data:usernamepasswordKVPairs];

从keychain中取出用户名和密码:

NSMutableDictionary *usernamepasswordKVPairs = (NSMutableDictionary *)[CHKeychain load:KEY_USERNAME_PASSWORD];

txtfldUsername.text = [usernamepasswordKVPairs objectForKey:KEY_USERNAME];

txtfldPassword.text = [usernamepasswordKVPairs objectForKey:KEY_PASSWORD];

删除一个keychain item:

[CHKeychain delete:KEY_USERNAME_PASSWORD];

这样一个简单的使用keychain存取用户名密码的功能就做好了。

方法二:

使用沙盒保存信息

这个方法相对于上个方法,更加简单,快捷,也不需要去建立一个类。直接保存取出就OK咯。

//取出保存好的名称和密码

_userNameTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:kUserName];

_passWordTF.text = [[NSUserDefaults standardUserDefaults] objectForKey:kPassWd];

[[NSUserDefaults standardUserDefaults] synchronize];

//保存需要保存的名称和密码

[[NSUserDefaults standardUserDefaults] setObject:_userNameTF.text forKey:kUserName];

[[NSUserDefaults standardUserDefaults] setObject:_passWordTF.text forKey:kPassWd];

[[NSUserDefaults standardUserDefaults] synchronize];

上一篇下一篇

猜你喜欢

热点阅读