iOS 手机记录登录账号密码列表

当在开发的过程中,我们可能需要记录一下登陆过的账号密码,为了用户方便登录时再次操作。

举个例子:当我们退出登录的时候,换一个账号登录的时候我们就可以在记录的账号列表中选择想要的账号,直接就可以登录了。可以方便用户登录。

这个功能很明显我们没有必要通过后台来实现,前台实现就可以了,那我们要怎么实现这个功能呢?

首先我们考虑的方式有很多,比较方便的就是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];

这样我们就可以实现获取记录的所有账号和密码,就可以实现用登录时获取记录

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王 哪跑!!!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值