#import <Foundation/Foundation.h>sql
#import "sqlite3.h"數據庫
@interface UserModel : NSObject數據結構
{ui
sqlite3 *link;atom
NSString *path;spa
}.net
//插入數據3d
-(void)insertMessageWithAccount:(NSString *)account andPassword:(NSString *)password andNickName:(NSString *)nickName;orm
//註冊時檢查帳號存在性sqlite
-(BOOL)jugdeExist:(NSString *)account;
//登陸操做
-(NSString *)loginMethod:(NSString *)account andPassword:(NSString *)password;
#import "UserModel.h"
@implementation UserModel
-(id)init{
self=[super init];
//定義數據庫設置
path=@"/Users/feifanchengxuyuan/Desktop/Forum.db";
//打開數據庫連接
sqlite3_open([path UTF8String], &link);
//建立用戶表的SQL語句
NSString *createUserTable=@"create table if not exists User(userId integer primary key autoincrement,account varchar(20),password varchar(20),nickName varchar(20))";
//執行SQL語句
sqlite3_exec(link, [createUserTable UTF8String],nil,nil,nil);
return self;
}
//插入方法
-(void)insertMessageWithAccount:(NSString *)account andPassword:(NSString *)password andNickName:(NSString *)nickName{
NSString *insertMessage=[NSString stringWithFormat:@"insert into User(account,password,nickName) values(\"%@\",\"%@\",\"%@\");",account,password,nickName];
sqlite3_exec(link, [insertMessage UTF8String] , nil,nil,nil);
}
/*
sqlite 操做二進制數據須要用一個輔助的數據類型:sqlite3_stmt * 。
這個數據類型 記錄了一個「sql語句」。爲何我把 「sql語句」 用雙引號引發來?由於你能夠把 sqlite3_stmt * 所表示的內容當作是 sql語句,可是實際上它不是咱們所熟知的sql語句。它是一個已經把sql語句解析了的、用sqlite本身標記記錄的內部數據結構。
sqlite3_stmt * stat;是把一個 sql 語句解析到 stat 結構裏去:
*/
//註冊時檢查帳號存在性
-(BOOL)jugdeExist:(NSString *)account{
sqlite3_stmt *stam;
// NSString *accountFromDataBase;
NSString *judge=[NSString stringWithFormat:@"select account from User where account=\"%@\";",account];
sqlite3_exec(link, [judge UTF8String],nil,nil,nil);
//進行預編譯
sqlite3_prepare_v2(link, [judge UTF8String], -1, &stam, nil);
//每條數據查找
while (sqlite3_step(stam)==SQLITE_ROW) {
return YES;
}
return NO;
}
//登陸操做
-(NSString *)loginMethod:(NSString *)account andPassword:(NSString *)password{
sqlite3_stmt *stmt;
NSString *userName=@"";
NSString * selectAccount=[NSString stringWithFormat:@"select * from User where account=\"%@\";",account];
sqlite3_prepare_v2(link, [selectAccount UTF8String], -1, &stmt, nil);
while (sqlite3_step(stmt)==SQLITE_ROW) {
NSString *passwordFromDataBase=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 2) encoding:NSUTF8StringEncoding];
if ([passwordFromDataBase isEqualToString:password]) {
userName=[[NSString alloc]initWithCString:(char *)sqlite3_column_text(stmt, 3) encoding:NSUTF8StringEncoding];
}
}
return userName;
}
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableView *listTableView;
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet UIButton *writeButton;
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (weak, nonatomic) IBOutlet UIButton *quitButton;
- (IBAction)quitMethod:(id)sender;
@end
#import "ViewController.h"
@interface ViewController ()
@property(nonatomic,strong)NSUserDefaults *userDefault;
@end
@implementation ViewController
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"-----viewDidAppear-----");
_userDefault=[NSUserDefaults standardUserDefaults];
NSString *userName=[_userDefault valueForKey:@"name"];
NSLog(@"登陸名:%@",userName);
if (userName == nil) {
_messageLabel.text=@"你好,遊客。發帖請登陸。";
_loginButton.hidden=NO;
_registerButton.hidden=NO;
_writeButton.hidden=YES;
_quitButton.hidden=YES;
}else{
_messageLabel.text=[NSString stringWithFormat:@"用戶:%@,歡迎回來",userName];
_loginButton.hidden=YES;
_registerButton.hidden=YES;
_writeButton.hidden=NO;
_quitButton.hidden=NO;
}
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"-----viewDidLoad-----");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)quitMethod:(id)sender {
NSLog(@"註銷方法");
[_userDefault removeObjectForKey:@"name"];
_messageLabel.text=@"你好,遊客。發帖請登陸。";
_loginButton.hidden=NO;
_registerButton.hidden=NO;
_writeButton.hidden=YES;
_quitButton.hidden=YES;
}
@end
@interface RegisterViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *accountTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *nickNameTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *passWordTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *writeAgainTextFeild;
- (IBAction)back:(id)sender;
- (IBAction)registMethod:(id)sender;
@end
#import "RegisterViewController.h"
#import "UserModel.h"
@interface RegisterViewController ()
@end
@implementation RegisterViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)registMethod:(id)sender {
UserModel *user=[[UserModel alloc]init];
NSString * userAccount=_accountTextFeild.text;
NSString *userPassword=_passWordTextFeild.text;
NSString *userAgain=_writeAgainTextFeild.text;
NSString *userNickName=_nickNameTextFeild.text;
if ([userAccount length]>=6&&[userAccount length]<=16&&[userPassword length]>=6&&[userPassword length]<=16&&[userAgain length]>=6&&[userAgain length]<=16&&[userNickName length]>=6&&[userNickName length]<=16) {
if ([userPassword isEqualToString:userAgain]) {
if ([user jugdeExist:userAccount]) {
NSLog(@"帳號存在");
}else{
[user insertMessageWithAccount:_accountTextFeild.text andPassword:_passWordTextFeild.text andNickName:_nickNameTextFeild.text];
}
}
else{
NSLog(@"兩次密碼輸入不一致");
}
}else {
NSLog(@"信息填寫不正確");
}
}
@end
#import <UIKit/UIKit.h>
@interface LoginViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *accountTextFeild;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextFeild;
@property (weak, nonatomic) IBOutlet UILabel *message;
- (IBAction)loginMethod:(UIButton *)sender;
- (IBAction)back:(id)sender;
@end
#import "LoginViewController.h"
#import "UserModel.h"
@interface LoginViewController ()
@end
@implementation LoginViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
}
- (IBAction)loginMethod:(UIButton *)sender {
UserModel *userModel=[[UserModel alloc]init];
NSUserDefaults *userDefault=[NSUserDefaults standardUserDefaults];
NSString *name=[userModel loginMethod:_accountTextFeild.text andPassword:_passwordTextFeild.text];
if (![name isEqualToString:@""]) {
[userDefault setValue:name forKey:@"name"];
[userDefault synchronize];
[self performSegueWithIdentifier:@"longinSuccess" sender:nil];
}else{
_message.text=@"登陸失敗,請重試";
}
NSLog(@"%@",[userDefault valueForKey:@"name"]);
// [self dismissViewControllerAnimated:NO completion:nil];
}
@end