0

私のアプリケーションで私がやっていることに気づきました。「Username」などのユーザー名を登録し、「username」でログインしようとするとログインできませんが、「Username」を試してみるとログインできます。誰かがそれを修正する方法を教えてもらえますか?これが私のコードです。登録:

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2
@synthesize tbPassword,tbRepass,tbUsername;
-(void) createTable: (NSString *) UserTable
      withUserField: (NSString *) UserField
      withPassField: (NSString *) PassField;
{
    char *err;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' " "TEXT PRIMARY KEY, '%@' TEXT);", UserTable, UserField,PassField];
    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
        sqlite3_close(users);
        NSAssert(0, @"Could not create table");

    }
    else{
        NSLog(@"table created");
    }
}

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"users.sql"];
}

-(void) openDB {
    if (sqlite3_open([[self filePath]UTF8String],&users) != SQLITE_OK){
        sqlite3_close(users);
        NSAssert(0, @"Database failed to open");
    }
    else{
        NSLog(@"database opened");
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self openDB];
    [self createTable:@"UserAccount" withUserField:@"Username" withPassField:@"Password"];
    self.tbUsername.delegate=self;
    self.tbPassword.delegate=self;
    self.tbRepass.delegate=self;
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnRegister:(id)sender
{
    NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s'",[tbUsername.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(users, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        if(sqlite3_step(statement)==SQLITE_ROW)
        {

            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Username already exist!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];

        }
        else
        {
           if ([tbUsername.text isEqualToString:@""])
            {

                UIAlertView *alert2 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please enter a username." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert2 show];
            }
            else if([tbPassword.text length]==0 || [tbRepass.text length]==0)
            {

                UIAlertView *alert3 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Please enter a password." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert3 show];
            }
            else if([tbPassword.text length]>0 && [tbRepass.text length]>0)
            {

                if([tbPassword.text isEqualToString:tbRepass.text])
                {
                    NSLog(@"Password Match");
                    NSString *username = tbUsername.text;
                    NSString *pass = tbPassword.text;

                    NSString *sql = [NSString stringWithFormat:@"INSERT INTO UserAccount ('Username','Password') VALUES ('%@','%@')",username,pass];
                    char *err;
                    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK)
                    {
                        sqlite3_close(users);
                        NSAssert(0, @"Could not update table)");

                    }
                    else
                    {
                        NSLog(@"table updated");
                        UIAlertView *alert4 = [[UIAlertView alloc]initWithTitle:@"Done" message:@"Account was registered successfully!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                        [alert4 show];
                        ViewController *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController"];
                        VC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

                       [self presentViewController:VC animated:YES completion:nil];

                        tbUsername.text = @"";
                        tbPassword.text = @"";


                    }

                }

                else
                {
                    UIAlertView *alert5 = [[UIAlertView alloc]initWithTitle:@"Error" message:@"Password does not match." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert5 show];
                }
            }
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];
    [[self tbRepass]resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end

ログインする:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize tbPassword,tbUsername;
-(void) createTable: (NSString *) UserTable
      withUserField: (NSString *) UserField
      withPassField: (NSString *) PassField;
{
    char *err;
    NSString *sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS '%@' ('%@' " "TEXT PRIMARY KEY, '%@' TEXT);", UserTable, UserField,PassField];
    if (sqlite3_exec(users, [sql UTF8String], NULL, NULL, &err)!=SQLITE_OK) {
        sqlite3_close(users);
        NSAssert(0, @"Could not create table");

    }
    else{
        NSLog(@"table created");
    }
}

-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"users.sql"];
}

-(void) openDB {
    if (sqlite3_open([[self filePath]UTF8String],&users) != SQLITE_OK){
        sqlite3_close(users);
        NSAssert(0, @"Database failed to open");
    }
    else{
        NSLog(@"database opened");
    }
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [self openDB];
    [self createTable:@"UserAccount" withUserField:@"Username" withPassField:@"Password"];
    self.tbUsername.delegate=self;
    self.tbPassword.delegate=self;
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnLogin:(id)sender {
    NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s' and Password = '%s'",[tbUsername.text UTF8String],[tbPassword.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(users, [select UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        if(sqlite3_step(statement)==SQLITE_ROW)
        {
            ViewController3 *VC = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController3"];
            VC.strUsername = self.tbUsername.text;
            [self presentViewController:VC animated:YES completion:nil];

        }
        else
        {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Authentication Failed" message:@"Wrong Username/Password!" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
        }

    }
}

- (IBAction)btnRegister:(id)sender {
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end

brbはクラスを取得しました。

4

2 に答える 2

3

クエリを変更します。

NSString *select = [NSString stringWithFormat:@"SELECT * FROM UserAccount WHERE Username = '%s' COLLATE NOCASE and Password = '%s'",[tbUsername.text UTF8String],[tbPassword.text UTF8String]];


これが役立つと思います。

于 2013-02-04T04:09:46.860 に答える
1

ログイン時viewController

初め

username.text {ユーザーがUIを使用して追加したユーザー名}を小文字に変更し、新しいNSStringに保存します

2番

SELECTステートメントで元のユーザー名と変更されたユーザー名NSStringの両方を使用する

注#where句では「または」を使用する必要があります

単純 :)

于 2013-02-04T07:05:33.330 に答える