如何检查从服务器数据库中的用户名和密码,并显示iphone新的一页 [英] How to check username and password from server database and display a new page in iphone

查看:241
本文介绍了如何检查从服务器数据库中的用户名和密码,并显示iphone新的一页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我有一个登录表单,其中包括用户名和password.When用户输入用户名和密码,用户名和密码应该从服务器API数据库进行验证,如果他是一个有效的用户,他应该提出的申请登录。
    我也做了以下code后,通过服务器API数据库的登录信息

i have an application in which i have a login form which consist of username and password.When user enters username and password his username and password should be validated from the server api database and if he is a valid user he should be made to login . i have done the following code to post the login information through server api database

 -(void)sendRequest
    {

        UIDevice *device = [UIDevice currentDevice];
        NSString *udid = [device uniqueIdentifier];
        NSString *sysname = [device systemName];
        NSString *sysver = [device systemVersion];
        NSString *model = [device model];
        NSLog(@"idis:%@",[device uniqueIdentifier]);
        NSLog(@"system nameis :%@",[device systemName]);
        NSLog(@"System version is:%@",[device systemVersion]);
        NSLog(@"System model is:%@",[device model]);
        NSLog(@"device orientation is:%d",[device orientation]);
        NSString *post = [NSString stringWithFormat:@"Loginkey=%@&Password=%@&DeviceCode=%@&Firmware=%@&IMEI=%@",txtUserName.text,txtPassword.text,model,sysver,udid];
        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 
        NSLog(@"%@",postLength);
        NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 
        [request setURL:[NSURL URLWithString:@"http://192.168.0.68:91/JourneyMapperAPI?RequestType=Login"]]; 
        [request setHTTPMethod:@"POST"]; 
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
        [request setHTTPBody:postData];

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

        if (theConnection) {
            webData = [[NSMutableData data] retain];
            NSLog(@"%@",webData);
        }
        else 
        {

        }

    }

在此方法,我分析从API服务器和有约束力和放大器收到JSON响应;插入SQLite数据库。

in this method i am parsing the JSON response received from api server and binding & inserting into sqlite database .

-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    {      
        NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding]; 
        NSLog(@"%@",loginStatus); 

        NSString *json_string = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; 

        NSDictionary *result = [json_string JSONValue];
        NSArray *values = [result objectForKey:@"Result"];
        NSMutableArray *results = [[NSMutableArray alloc] init];

        for (int index = 0; index<[values count]; index++) {
            NSMutableDictionary * value = [values objectAtIndex:index];
            Result * result = [[Result alloc] init];
            result.UserID = [value objectForKey:@"UserId"];
            result.FirstName = [value objectForKey:@"FirstName"];
            result.LastName =[value objectForKey:@"LastName"];
            result.Email =[value objectForKey:@"Email"];
            result.ProfileImage =[value objectForKey:@"ProfileImage"];
            result.ThumbnailImage =[value objectForKey:@"ThumbnailImage"];
            result.DeviceInfoId =[value objectForKey:@"DeviceInfoId"];
            NSLog(@"%@",result.UserID);


            [results addObject:result];
            [result release]; 
        }



        for (int index = 0; index<[results count]; index++) {
            Result * result = [results objectAtIndex:index];
            //save the object variables to database here


            [self createEditableCopyOfDatabaseIfNeeded];

            NSString *filePath = [self getWritableDBPath];

            sqlite3 *database;
            NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970]; 
            NSNumber *timeStampObj = [NSNumber numberWithInt: timeStamp];
            NSLog(@"%@",timeStampObj);
            NSString *journeyid = [NSString stringWithFormat:@"%@_%@_%@", result.UserID, result.DeviceInfoId, timeStampObj];
            if(sqlite3_open([filePath UTF8String], &database) == SQLITE_OK) {
                const char *sqlStatement = "insert into UserInformation(journeyid,UserID,DeviceId,Username,Password,FirstName,Email) VALUES (?,?,?,?,?,?,?)";
                sqlite3_stmt *compiledStatement;
                if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK)    {
                    sqlite3_bind_text( compiledStatement, 1, [journeyid UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text( compiledStatement, 2, [result.UserID UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text(compiledStatement, 3, [result.DeviceInfoId UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text(compiledStatement, 4, [txtUserName.text UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text(compiledStatement, 5, [txtPassword.text UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text (compiledStatement, 6, [result.FirstName UTF8String],-1,SQLITE_TRANSIENT);
                    sqlite3_bind_text (compiledStatement, 7, [result.Email UTF8String],-1,SQLITE_TRANSIENT);

                }
                if(sqlite3_step(compiledStatement) != SQLITE_DONE ) {
                    NSLog( @"Save Error: %s", sqlite3_errmsg(database) );
                }
                else {
                    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"UIAlertView" message:@"Record added" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
                    [alert show];
                    [alert release];
                    alert = nil;
                }

                sqlite3_finalize(compiledStatement);
            }
            sqlite3_close(database);    
        }
        [loginStatus release];           
        [connection release];  
        [webData release]; 
    }  

问题是我想,当用户输入他/她的用户名和密码的用户名和密码,应该从服务器数据库进行验证,如果用户是有效的,他应该被允许登录。请帮我解决这个问题.Thanks

The problem is i want that when a user enters his/her username and password the username and password should be validated from the server database and if the user is valid he should be allowed to login .Please help me in solving this problem.Thanks

推荐答案

更改您的JSON响应,使用一些其他的键发送的登录成功/失败的响应。检查使用

Change your JSON response, Use some other key to send the response for login success/Fail. Check using the

NSString *loginres=[result valueForKey:@"LoginResponse"];

然后验证使用

if([loginres isEqualToString:@"Success"]
{
  //Valid user
}
else
{
  //InValid user
}

如果验证失败让的NSArray *值= [结果objectForKey:@结果]; 为NULL

If the authentication fail let the NSArray *values = [result objectForKey:@"Result"]; be NULL

然后检查

if(values!=NULL)
{
      //Valid user
}
else
{
      //InValid user
}

这篇关于如何检查从服务器数据库中的用户名和密码,并显示iphone新的一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆