当在开发的过程中,我们可能需要记录一下登陆过的账号密码,为了用户方便登录时再次操作。
举个例子:当我们退出登录的时候,换一个账号登录的时候我们就可以在记录的账号列表中选择想要的账号,直接就可以登录了。可以方便用户登录。
这个功能很明显我们没有必要通过后台来实现,前台实现就可以了,那我们要怎么实现这个功能呢?
首先我们考虑的方式有很多,比较方便的就是plist文件和数据库,但是很明显,我们创建plist文件更简单,可以在沙盒路径下或者工程路径下直接查看
我们已plist文件为例,键值对中key为账号名称,value为账号密码,当然为了方便使用我们要创建单例。下面是实现代码,首先是.h文件
#import <Foundation/Foundation.h>
@interface UserNameAndPassWord : NSObject
/**
创建单例,返回始终为一个对象
@return self
*/
+ (UserNameAndPassWord *)sharedManager;
/**
返回所有数据
@return 字典
*/
- (NSMutableDictionary*)getAllNameAndPassWord;
/**
根据key来删除某一行的数据
@param key 所需要的key
*/
- (void)deleteDataWithDataKey:(NSString *)userName;
/**
plist 文件的数据插入,将用户名和密码进行储存
@param name 有户名
@param passWord 密码
*/
- (void)insertUserName:(NSString*)name withPassWord:(NSString*)passWord;
@end
接下来是.m文件
#import "UserNameAndPassWord.h"
@implementation UserNameAndPassWord
/**
创建单例,返回始终为一个对象
@return self
*/
+ (UserNameAndPassWord *)sharedManager{
static UserNameAndPassWord *httpRequst = nil;
static dispatch_once_t predicate;
dispatch_once(&predicate, ^{
httpRequst = [[self alloc] init];
[self creatUserNameAndPassWordPList];
});
return httpRequst;
}
/**
创建plist文件,进行用户数据储存
*/
+ (void)creatUserNameAndPassWordPList{
NSString *plistPath = [PATH_STRING stringByAppendingPathComponent:@"UserNameAndPassWord.plist"];
//判断是否已经创建文件
if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
NSLog(@"UserNameAndPassWord.plist文件已经存在!");
}else {
//plist文件没有被创建
NSMutableDictionary *rootDic = [[NSMutableDictionary alloc] init];
NSMutableDictionary *subDic = [[NSMutableDictionary alloc] init];
[rootDic setObject:subDic forKey:@"UserNameAndPassWord"];
//写入到文件
[rootDic writeToFile:plistPath atomically:YES];
}
}
/**
返回所有数据
@return 数组
*/
- (NSMutableDictionary*)getAllNameAndPassWord{
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"UserNameAndPassWord.plist"];
NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];
return [rootDic objectForKey:@"UserNameAndPassWord"];
}
/**
根据key来删除某一行的数据
@param key 所需要的key
*/
- (void)deleteDataWithDataKey:(NSString *)userName{
//获取路径
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"UserNameAndPassWord.plist"];
NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];
NSMutableDictionary *studentInfo = [rootDic objectForKey:@"UserNameAndPassWord"];
[studentInfo removeObjectForKey:userName];
[rootDic setValue:studentInfo forKey:@"UserNameAndPassWord"];
//写入到文件
[rootDic writeToFile:path atomically:YES];
}
/**
plist 文件的数据插入,将用户名和密码进行储存
@param name 有户名
@param passWord 密码
*/
- (void)insertUserName:(NSString*)name withPassWord:(NSString*)passWord{
//获取路径
NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:@"UserNameAndPassWord.plist"];
NSMutableDictionary *rootDic = [[[NSMutableDictionary alloc] initWithContentsOfFile:path] mutableCopy];
NSMutableDictionary *studentInfo = [rootDic objectForKey:@"UserNameAndPassWord"];
NSString *userPassWord = [studentInfo objectForKey:name];
userPassWord = passWord;
[studentInfo setValue:userPassWord forKey:name];
//写入到文件
[rootDic writeToFile:path atomically:YES];
}
以上就实现了数据的插入、删除和获取,下面是使用代码
首先获取数据:
@property (strong, nonatomic) NSMutableDictionary *dataSource;
//获取所有数据,里面数据存储形式, key 为 账户名 value 为 密码
_dataSource = [[UserNameAndPassWord sharedManager]getAllNameAndPassWord];
下面是删除数据:
[[UserNameAndPassWord sharedManager]deleteDataWithDataKey:@"用户名"];
下面是添加用户账号和密码:
[[UserNameAndPassWord sharedManager]insertUserName:phoneNumber withPassWord:passWord];
这样我们就可以实现获取记录的所有账号和密码,就可以实现用登录时获取记录