[课堂实践与项目]手机QQ客户端--4期(SQLite的加入,注册,找回,登录界面的修改):建立关于QQ注册类,使用SQLite进行存储,
经过昨天下午和今天上午的不懈努力,终于通过了SQLite的学习。
我们现在这里定义一个有关SQLIte的封装类,便于我在后面的用户注册,用户密码找回,和登录界面的使用
1.首先我们看看我们建立的userModel,虽然很多属性没有使用,但是作为备用
LCUserMessageModel.h文件。
#import <Foundation/Foundation.h> @interface LCUserMessageModel : NSObject<NSCoding> @property (assign,nonatomic)NSInteger QQ; @property (copy,nonatomic)NSString *userName; @property (copy,nonatomic)NSString *userAddress; @property (copy,nonatomic)NSString *userMobilephineNumber; @property (copy,nonatomic)NSString *userPassword; @property (copy,nonatomic)NSString *userRePassword; @property (copy,nonatomic)NSString *userStuID; @property (copy,nonatomic)NSString *userSeriousFriendName; @property (copy,nonatomic)NSString *userPIDSixNumber; @end
LCUserMessageModel.m文件,要说明的是,如果我是用了SQLite进行存储,那么就不需要NSCODing协议了,但是我使用的原因是因为我多次修改,使用了文件存储的方法。
// // LCUserMessageModel.m // 手机QQ客户端 // // Created by lichan on 13-12-11. // Copyright (c) 2013年 com.lichan. All rights reserved. // #import "LCUserMessageModel.h" @implementation LCUserMessageModel @synthesize QQ, userAddress,userMobilephineNumber,userName,userPassword,userPIDSixNumber,userRePassword,userSeriousFriendName,userStuID; - (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeInteger:QQ forKey:@"QQ"]; [aCoder encodeObject:userName forKey:@"userName"]; [aCoder encodeObject:userAddress forKey:@"userAddress"]; [aCoder encodeObject:userMobilephineNumber forKey:@"userMobilephineNumber"]; [aCoder encodeObject:userPassword forKey:@"userPassword"]; [aCoder encodeObject:userRePassword forKey:@"userRePassword"]; [aCoder encodeObject:userStuID forKey:@"userStuID"]; [aCoder encodeObject:userSeriousFriendName forKey:@"userSeriousFriendName"]; [aCoder encodeObject:userPIDSixNumber forKey:@"userPIDSixNumber"]; } - (id)initWithCoder:(NSCoder *)aDecoder { if (self = [super init]) { self.QQ = [aDecoder decodeIntegerForKey:@"QQ"]; self.userName = [aDecoder decodeObjectForKey:@"userName"]; self.userAddress = [aDecoder decodeObjectForKey:@"userAddress"]; self.userMobilephineNumber = [aDecoder decodeObjectForKey:@"userMobilephineNumber"]; self.userPassword = [aDecoder decodeObjectForKey:@"userPassword"]; self.userRePassword = [aDecoder decodeObjectForKey:@"userRePassword"]; self.userStuID = [aDecoder decodeObjectForKey:@"userStuID"]; self.userSeriousFriendName = [aDecoder decodeObjectForKey:@"userSeriousFriendName"]; self.userPIDSixNumber = [aDecoder decodeObjectForKey:@"userPIDSixNumber"]; } return self; } -(NSString *)description { return [NSString stringWithFormat:@"QQ:%d password:%@",self.QQ,self.userPassword]; } @end
2.关于SQLite进行属性封装的类。很明显,我继承了NSOBject类型。注释给予说明
#import <Foundation/Foundation.h> #import <sqlite3.h> @class LCUserMessageModel; @interface LCSQLite3Methods : NSObject { sqlite3 *database; NSError *error; NSString *DBFilePath; } - (LCUserMessageModel *)queryFromDB; //查找所有的QQ注册信息,很多时候用不上 - (NSInteger)insertORUpdateToDB:(LCUserMessageModel *)user;//注册账号的使用调用 - (NSMutableArray *)findUserPasswordByMobilePhoneNumber:(NSString *)phoneNumber;//找回密码界面调用此方法 - (NSMutableArray *)findUserPasswordByQQ:(NSInteger )qq Password:(NSString *)password;//用户登录时候调用此方法 @end
3.密码找回界面,SQLite接口类的使用
1)LCFInd.h文件,不再赘述
#import <UIKit/UIKit.h> @class LCSQLite3Methods; @interface LCFindPSWViewController : UIViewController @property (weak, nonatomic) IBOutlet UITextField *mobilePhoneNumber; @property (strong,nonatomic)LCSQLite3Methods *SQLiteMethods; - (IBAction)findPSWButtonPressed:(id)sender; -(IBAction)textFieldDoneEditing:(id)sender; @end
2)实现文件,由于我只能依赖手机号进行找回,除非用户写入了QQ。唯一保险的办法是直接找回所有通过此手机号注册的所有QQ和密码。后续会完善此BUG。
#import "LCFindPSWViewController.h" #import "LCSQLite3Methods.h" #import "LCUserMessageModel.h" @interface LCFindPSWViewController () @property (strong,nonatomic)LCUserMessageModel *userModel; @end @implementation LCFindPSWViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { self.title = @"密码找回"; // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; self.navigationController.navigationBarHidden = NO; self.SQLiteMethods = [[LCSQLite3Methods alloc]init]; // Do any additional setup after loading the view from its nib. } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)findPSWButtonPressed:(id)sender { NSString *MPnumber = self.mobilePhoneNumber.text; NSMutableArray *findArray =[self.SQLiteMethods findUserPasswordByMobilePhoneNumber:MPnumber]; NSString *message = [NSString stringWithFormat:@"此手机号注册的QQ号码:%@",findArray]; UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"恭喜您,密码找回成功!" message:message delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil]; [alertView show]; // self.userModel = [[LCUserMessageModel alloc]init]; // // NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES) objectAtIndex:0]; // NSString *userFilePath = [path stringByAppendingPathComponent:self.mobilePhoneNumber.text]; // // NSMutableData *data = [NSMutableData dataWithContentsOfFile:userFilePath]; // // self.userModel = [NSKeyedUnarchiver unarchiveObjectWithData:data]; // // NSString *MPnumber = self.mobilePhoneNumber.text; // // NSString *message = [NSString stringWithFormat:@"已初始化账号为%@的密码为%@,请尽快修改密码。",MPnumber,self.userModel.userPassword]; // UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"恭喜您,密码找回成功!" message:message delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil]; // // [alertView show]; } #pragma mark 取消键盘响应的方法 -(IBAction)textFieldDoneEditing:(id)sender { [sender resignFirstResponder]; } @end