我正在尝试创建使用sqlite在iPhone中创建登录页面。 我需要建议,如何使用用户名/密码身份验证创建登录表单。为了成功登录 - 用户应该像UITableview一样导航到下一页。否则会向用户提供错误消息:登录失败。请帮我。我是编程新手。
答案 0 :(得分: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
}
我希望这段代码对你有用。
答案 1 :(得分:0)
-(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"
}
}
答案 2 :(得分:0)
您需要做的第一件事是将libsqlite3.dylib
添加到您的框架列表中。
作为第二件事,您应该开始阅读SQLite3 documentation,尤其是five-minute guide。如果您想要一些参考,以了解如何“将数据保存到SQLite数据库”和“从SQLite数据库中提取数据”,您可以随时查看Beautiful Tutorial:{ {3}}
第三次和大多数导入您需要做的事情是:阅读以上链接(参考问题中的行 I AM new to the Programming
)。
最后,我可以说你:祝你好运!!!
答案 3 :(得分:0)
首先使用UserName&User;创建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);
}
可能,它会帮助你。
感谢。