我有一个仪表板 ViewController,它检查数据库是否存在,如果不存在,则将用户发送到登录 ViewController,然后在其中创建数据库。然后用户必须登录,并且他们的密码和用户名被保存到 SQLite 数据库中。然后它们被发送到仪表板 ViewController。这样做的问题是,一旦用户确实登录并被发送回仪表板 ViewController,他们就会被拒绝并被发送回登录 ViewController。我不知道信息是否没有保存到数据库中或发生了什么但这是我的登录 m 文件
#import "LoginView.h"
#import "SBJson.h"
#import "SignupView.h"
#import "sqlite3.h"
@interface LoginView ()
@end
@implementation LoginView
@synthesize txtPassword;
@synthesize txtUsername;
-(void)viewDidLoad {
NSString *docsDir;
NSArray *dirPaths;
// Get the documents directory
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPaths objectAtIndex:0];
// Build the path to the database file
databasePath = [[NSString alloc] initWithString: [docsDir stringByAppendingPathComponent: @"username.db"]];
NSFileManager *filemgr = [NSFileManager defaultManager];
if ([filemgr fileExistsAtPath: databasePath ] == NO)
{
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &usernameDB) == SQLITE_OK)
{
char *errMsg;
const char *sql_stmt = "CREATE TABLE IF NOT EXISTS USERNAME (USERNAME TEXT, PASSWORD TEXT)";
if (sqlite3_exec(usernameDB, sql_stmt, NULL, NULL, &errMsg) != SQLITE_OK)
{
}
sqlite3_close(usernameDB);
} else {
}
}
[super viewDidLoad];
}
- (void) alertStatus:(NSString *)msg :(NSString *)title
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alertView show];
}
- (IBAction)signupButton:(id)sender {
SignupView *myView = [[SignupView alloc] initWithNibName:@"SignupView" bundle:nil];
[myView setModalPresentationStyle:UIModalPresentationFormSheet]; //you can change the way it is presented
[myView setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; //you can change the animation
[self presentViewController:myView animated:YES completion:nil]; //show the modal view
}
- (IBAction)backgroundClicked:(id)sender {
[txtUsername resignFirstResponder];
[txtPassword resignFirstResponder];
}
- (IBAction)loginClicked:(id)sender {
@try {
if([[txtUsername text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[txtUsername text],[txtPassword text]];
NSLog(@"PostData: %@",post);
NSURL *url=[NSURL URLWithString:@"http://example.com/ios_login/index.php"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%d",success);
if(success == 1)
{
[self saveData];
} else {
NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
[self alertStatus:error_msg :@"Login Failed!"];
}
} else {
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Login Failed!"];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Login Failed." :@"Login Failed!"];
}
}
-(void)saveData{
sqlite3_stmt *statement;
const char *dbpath = [databasePath UTF8String];
if (sqlite3_open(dbpath, &usernameDB) == SQLITE_OK)
{
NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO USERNAME (username, password) VALUES (\"%@\", \"%@\")", txtUsername.text, txtPassword.text];
const char *insert_stmt = [insertSQL UTF8String];
sqlite3_prepare_v2(usernameDB, insert_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
}
sqlite3_finalize(statement);
sqlite3_close(usernameDB);
[self dismissViewControllerAnimated:YES completion:nil];
}
}
@end
这是我的仪表板 M
#import "ViewController.h"
#import "LoginView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidAppear:(BOOL)animated {
[self checkIfLogged];
}
- (void) checkIfLogged
{
const char *dbpath = [databasePath UTF8String];
sqlite3_stmt *statement;
if (sqlite3_open(dbpath, &usernameDB) == SQLITE_OK) //second if
{
NSString *querySQL = [NSString stringWithFormat: @"SELECT * FROM username"];
const char *query_stmt = [querySQL UTF8String];
if (sqlite3_prepare_v2(usernameDB, query_stmt, -1, &statement, NULL) == SQLITE_OK)
{
if (sqlite3_step(statement) == SQLITE_ROW)
{//match found
} else {// match not found
LoginView *loginView = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
[loginView setModalPresentationStyle:UIModalPresentationFormSheet];
[loginView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:loginView animated:YES completion:nil];
}//end else
sqlite3_finalize(statement);
}//end second if
else{
LoginView *loginView = [[LoginView alloc] initWithNibName:@"LoginView" bundle:nil];
[loginView setModalPresentationStyle:UIModalPresentationFormSheet];
[loginView setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:loginView animated:YES completion:nil];
}
sqlite3_close(usernameDB);
}//end first IF
}//end checkIfLogged
@end
我感觉信息没有保存到数据库中,但我不知道为什么。信息应保存在 saveData() 下的登录视图中