登录界面多个账户信息存储问题
来源:互联网 发布:河南数据统计采集门户 编辑:程序博客网 时间:2024/04/30 09:16
方法一:使用归档:其实质就是读写文件操作,(归档,解档:archive,unarchive)
// 归档+ (NSData *)archivedDataWithRootObject:(id)rootObject;+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
// 解档+ (id)unarchiveObjectWithData:(NSData *)data;+ (id)unarchiveObjectWithFile:(NSString *)path;
归档写入文件有两种:
1.一个文件只保存一个对象
此种情况就是上面的简单归档,解档,如上两段代码。
2.一个文件保存多个对象
此种情况就是相当于写文件操作,
具体看以下代码:
-(void)saveDoctorInfo:(DoctorModel *)doctor;
-(DoctorModel *)getDoctorInfo:(NSString *)accid;
注:
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
/ /创建NSKeyedArchiver实例,用于将对象归档到此theData实例中。
一个文件保存多个用户带来的问题有:(1.)可以按顺序存储用户数据,但是不好限制保存个数(假如限制保存三个用户信息)
对于文件中的内容操作,只有读写,但是如果保存的用户越来越多,文件会越来越大。
如果要删除文件中的数据(字符串)只能用指针来实现。
第一种是用空字符串覆盖掉你所需要修改的数据。第二种是找到初始指针,往后一直写,数据便会覆盖掉。但是所要删除的对象数据结束指针位置难以知道。如果要删除的对象是第二个,则很有可能会把第三个用户信息覆盖掉。
(2.)无法按顺序取出保存的用户:
因为存储,查询文件中的对象使用的方法为:
- (void)encodeObject:(id)objv forKey:(NSString *)key;
- (id)decodeObjectForKey:(NSString *)key;
相当于文件中存储对象的方式是字典类型的,无法像数组那样获得顺序
//// DoctorManager.m// com.yx129.yxClientDoctor3//// Created by yx on 15/2/11.// Copyright (c) 2015年 Guangzhou Yixiang Internet Technology Development Limited. All rights reserved.//#define fileName_LastDoctorInfo @"lastDoctor.info"#define fileName_SavedDoctorInfo @"savedDoctor.info"//#define klastDoctorEncodeKey @"lastDoctor"#define ksavedDoctorEncodeKey @"savedDoctor"#import "DoctorManager.h"@implementation DoctorManager{// NSMutableDictionary * _lastDoctorInfoDic;// NSString *_lastDoctorInfoPath;}static DoctorManager * manager=nil;static DoctorModel *lastDoctor = nil;static DoctorModel *toolDoctor = nil;static DoctorModel *savedDoctor = nil;+(id)shareDoctorManager{ static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ manager=[[DoctorManager alloc]init]; toolDoctor = [[DoctorModel alloc] init]; }); return manager;}+(DoctorModel *)getSavedDoctor{ if (savedDoctor == nil){ savedDoctor = [[DoctorModel alloc] init]; } return savedDoctor;}-(id)init{ if (self=[super init]) { // _lastDoctorInfoPath=[NSString stringWithFormat:@"%@/Documents/lastDoctorInfo.plist",NSHomeDirectory()]; } return self; }-(void)saveLastLoginDoctor:(DoctorModel *)doctor{ // NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];// NSString *path=[docPath stringByAppendingPathComponent:fileName_LastDoctorInfo]; NSString *path =[self getFilePath:fileName_LastDoctorInfo]; [NSKeyedArchiver archiveRootObject:doctor toFile:fileName_LastDoctorInfo];}-(DoctorModel *)getLastLoginDoctor{ if (!lastDoctor) { lastDoctor=[[DoctorModel alloc]init]; } // NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];// NSString *path=[docPath stringByAppendingPathComponent:fileName_LastDoctorInfo]; NSString *path =[self getFilePath:fileName_LastDoctorInfo]; lastDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; return lastDoctor;}#pragma 保存账户信息-(void)saveDoctorInfo:(DoctorModel *)doctor{ NSMutableData *data = [[NSMutableData alloc] init]; NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data]; [archiver encodeObject:doctor forKey:doctor.acc_id]; [archiver finishEncoding]; [data writeToFile:[self getFilePath:fileName_SavedDoctorInfo] atomically:YES];}-(DoctorModel *)getDoctorInfo:(NSString *)accid{ if ([[NSFileManager defaultManager] fileExistsAtPath:[self getFilePath:fileName_SavedDoctorInfo]]) { NSData *data = [[NSData alloc] initWithContentsOfFile:[self getFilePath:fileName_SavedDoctorInfo]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; if (!lastDoctor) { lastDoctor=[[DoctorModel alloc]init]; } lastDoctor = [unarchiver decodeObjectForKey:accid]; [unarchiver finishDecoding]; } return lastDoctor;}-(DoctorModel *)getLastSavedDoctorInfo{ DoctorModel *lastSavedDoctor=[[DoctorModel alloc]init]; lastSavedDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:[self getFilePath:fileName_LastDoctorInfo]]; return lastSavedDoctor;}-(NSString *)getFilePath:(NSString *)fileName{ NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];}/*-(void)saveLastLoginDoctor:(DoctorModel *)doctor{ NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; NSString *path=[docPath stringByAppendingPathComponent:@"lastDoctor.info"]; NSLog(@"path=%@",path); toolDoctor = doctor; [NSKeyedArchiver archiveRootObject:toolDoctor toFile:path];}-(DoctorModel *)getLastLoginDoctor{ if (!lastDoctor) { lastDoctor=[[DoctorModel alloc]init]; // 设置状态,是否已经进入信息详情页面 NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject]; NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"]; NSLog(@"path=%@",path); toolDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; } lastDoctor = toolDoctor; // if (!lastDoctor) {// lastDoctor=[[DoctorModel alloc]init]; // 设置状态,是否已经进入信息详情页面// // NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];// NSString *path=[docPath stringByAppendingPathComponent:@"person.yangyang"];// NSLog(@"path=%@",path);// // lastDoctor = [NSKeyedUnarchiver unarchiveObjectWithFile:path];// } return lastDoctor;}//-(void)synchronize//{// [_lastDoctorInfoDic writeToFile:_lastDoctorInfoPath atomically:YES];// // [_lastDoctorInfoDic removeAllObjects];// //} */@end
由于以上原因,当你需要增删查改多个对象时,不能采用直接保存多个对象形式,而要将对象用数组包装,再将这个数组写入归档。
这时候根对象是数组。
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
-(void)saveDoctor:(DoctorModel *)doctor{ NSString *path =[self getFilePath:fileName_SavedDoctorInfo]; NSMutableArray *doctorsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; if (doctorsArr == nil) { doctorsArr = [NSMutableArray arrayWithObject:doctor]; }else{ if (doctorsArr.count >= 5) [doctorsArr removeObjectAtIndex:0]; [doctorsArr addObject:doctor]; } [NSKeyedArchiver archiveRootObject:doctorsArr toFile:fileName_SavedDoctorInfo];}-(DoctorModel *)getDoctor:(NSString *)accid{ NSString *path =[self getFilePath:fileName_SavedDoctorInfo]; NSMutableArray *doctorsArr = [NSKeyedUnarchiver unarchiveObjectWithFile:path]; DoctorModel *aDoctor; if (doctorsArr != nil) { for (DoctorModel *savedDoc in doctorsArr) { if ([savedDoc.acc_id isEqualToString:accid]) { aDoctor = savedDoc; break; } } } return aDoctor;}-(NSString *)getFilePath:(NSString *)fileName{ NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); return [[array objectAtIndex:0] stringByAppendingPathComponent:fileName];}
方法二:用plist
需要用将数据加密后再进行存储
md5 只能 加密不能解密
应用 aes 加密算法
此种方法,排序是个问题
- 登录界面多个账户信息存储问题
- 利用Mysql数据库存储账户信息,C#编写登录界面
- 解决同一浏览器登录多个账户session共享问题
- git配置多个账户登录
- 解决windows server 同一账户远程登录出现多个桌面回话问题
- xrdp+vnc多账户登录黑屏问题
- 账户信息(注册、登录)
- Mac 登录界面多了一个其他账户删除
- Mac 登录界面多了一个其他账户删除
- Android——登录界面、SharedPreferences实现记住密码等账户信息
- Android——登录界面、SharedPreferences实现记住密码等账户信息
- 登录界面保存信息
- 多个账户模拟登录---c#异步模拟登录网站并采集网页
- 多个账户模拟登录---c#异步模拟登录网站并采集网页
- android信息从联系人添加收件人,在联系人多选界面(联系人中有Google账户)搜索时,搜索结果出现排序错误的问题
- Mac系统开发常见问题-Mac登录界面多了一个其他账户选项
- 关于Google多账户登录
- 同一个电脑连接多个github账户的问题
- 我是如何自学Android,资料分享(2015 版)
- iOS学习笔记(5)——设计模式之工厂方法、抽象工厂
- spring加载bean(ClassPathXmlApplicationContext和FileSystemXmlApplicationContext)
- 总结之linux下的操作
- java得到当月天数
- 登录界面多个账户信息存储问题
- UICollectionView详解
- Android 网络学习总结
- AngularJS 无限滚动加载数据控件 ngInfiniteScroll
- Vmware vSphere(Esxi)常见问题汇总
- 2014十大顶级开源ERP系统点评
- Spring中Ioc入门实例
- 快速建立列表对话框
- Majority Element (leetcode )