-1

我正在尝试使用 sqlite 在 iPhone 中创建登录页面。我需要建议,如何使用用户名/密码身份验证创建登录表单。为了成功登录 - 用户应该导航到 UITableview 之类的下一页。否则会给用户一条错误消息:登录失败。请帮我。我是编程新手。

4

4 回答 4

1

首先,您创建注册页面,然后将此代码应用于您的登录视图..

if([uername.text isEqualToString:@""] || [password.text isEqualToString:@""]) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Here comes a block." message:@"You missed something, please check your details and try again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    [alert release];
    }
else
    {
    //insert data

    NSString *query=[NSString stringWithFormat:@"select * from registration_table where username=\"%@\" and password=\"%@\"",uername.text,password.text];
      NSLog(@"%@",query);

      //select * from registration_tbl where username="d" and password="d"

        array=[app.sk lookupAllForSQL:query];
        NSLog(@"%@",[array description]);
        if (array.count>0) 
            {
            app.currrentid=[[array objectAtIndex:0]objectForKey:@"uid"];
                NSLog(@"%@",app.currrentid);
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"" message:@"login successful" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
                [alert show];
                [alert release];
                                    messageviewcontroller *log = [[messageviewcontroller alloc]initWithNibName:@"messageviewcontroller" bundle:nil];
                [self.navigationController pushViewController:log animated:YES];
                [log release];
                log.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
                [self presentModalViewController:log animated:YES];

                }
        else 
            {
                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Here comes a block." message:@"angry" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
                [alert release];

                }


        uername.text=@"";
        password.text=@"";

        //push the view
        }

我希望这段代码对你有用。

于 2013-06-07T07:01:01.587 回答
0
  1. 创建用于存储用户名和密码的表
  2. 输入密码以加密的值。
  3. 使用 Textfield 为用户名和密码创建一个登录页面
  4. 在按钮操作上从文本字段中收集用户名和密码

.

-(IBAction)loginbuttonPressed:(id)sender
    {
        if (check username is in table == success)
        {
            //encrypt the password entered compare and check for password is true for the username in table
            if(check password is in table= password found success)
            {
                       // Successfully logged in
            }
            else
            {
                //alert "Password Incorrect"
            }
        }
        else
        {
            // Alert "Username not found"
        }
    }
于 2013-06-07T06:47:58.910 回答
0

您需要做的第一件事libsqlite3.dylib是添加到您的框架列表中。

作为第二件事,您应该开始阅读SQLite3 文档,尤其是五分钟指南。如果您想要一些参考来了解如何“将数据保存到 SQLite 数据库”“从 SQLite 数据库中提取数据”,您可以随时查看美丽教程:基于 SQLite 的 iPhone 应用程序示例

您需要做的第三件事也是最重要的事情是:阅读以上链接(在您的问题中引用 Line I AM new to the Programming)。

最后,我可以说你:祝你好运!!!

于 2013-06-07T06:48:15.317 回答
0

首先使用带有用户名和密码字段的用户表创建 1 个示例数据库。

然后,在第一次安装时将数据库复制到应用程序中。

- (void) copyDatabaseIfNeeded 
{
   NSFileManager *fileManager = [NSFileManager defaultManager];
   NSError *error;
   dbPath = [self getDBPath];
   BOOL success = [fileManager fileExistsAtPath:dbPath];

   if(!success) 
   {
       NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"dbSample.sqlite3"];
       success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
       if (!success)
           NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
   }
}

/********* Database Path *********/ 
- (NSString *) getDBPath 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *documentsDir = [paths objectAtIndex:0];

    return [documentsDir stringByAppendingPathComponent:@"dbSample.sqlite3"];
}

然后,检查验证按钮单击,

-(IBAction)validateUser:(id)sender
{
    if (sqlite3_open([[appDel getDBPath] UTF8String], &dbTest) == SQLITE_OK) 
    {
        NSString *querySQL = [NSString stringWithFormat:@"SELECT UserName, Password FROM tblUser Where UserName = \"%@\"",[txtUserName text]];
        NSLog(@"%@",querySQL);
        const char *sql = [querySQL UTF8String];
        sqlite3_stmt *searchStatement;
        if (sqlite3_prepare_v2(dbTest, sql, -1, &searchStatement, NULL) == SQLITE_OK) 
        {
            if (sqlite3_step(searchStatement) == SQLITE_ROW) 
            {
               infoUserName = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(searchStatement, 0)];
               infoUserPassword = [[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(searchStatement, 1)];
               NSLog(@"User : %@, Password : %@",infoUserName, infoPassword);
            }
        }

        sqlite3_finalize(searchStatement);
    }
    sqlite3_close(dbTest);
}

可能,它会帮助你。

谢谢。

于 2013-06-07T06:52:07.880 回答