2011-06-17 22 views
1

我有一个应用程序,其中有一个包含用户名和密码的登录表单。当用户输入用户名和密码时,应验证其用户名和密码从服务器API数据库,如果他是一个有效的用户,他应该作出登录。 我已经做下面的代码后在该方法我解析从API服务器接收到的JSON响应和结合&插入SQLite数据库通过服务器API数据库如何从服务器数据库检查用户名和密码,并在iphone中显示新页面

-(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 
     { 

     } 

    } 

的登录信息。

-(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]; 
    } 

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

+0

你在这段代码中面临什么问题? – iAmitWagh 2011-06-17 05:56:04

+0

是什么问题? – 2011-06-17 06:05:56

+0

如果用户有效或无效,则消息应由服务器在JSON或从JSON创建的字典中返回。如果用户无效,您能告诉我们服务器返回的内容吗? – 2011-06-17 06:14:33

回答

0

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

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

然后使用

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

OR

如果认证失败让NSArray *values = [result objectForKey:@"Result"];为NULL验证

然后检查

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

嗨@KingofBliss,我试过你的代码,但他们是一个问题。当我输入错误的用户名和密码时,它也显示它是一个有效的用户 – Rani 2011-06-17 06:58:17

+0

然后问题与您的websrvice。请改变它 – KingofBliss 2011-06-17 06:59:13

+0

你能告诉我问题在哪里。我尝试过这样的代码\t \t NSString * json_string = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding]; \t \t NSDictionary * result = [json_string JSONValue]; \t NSArray * values = [result objectForKey:@“Result”]; \t if(values!= NULL) \t { \t \t NSLog(@“Valid User”); \t} \t别的 \t { \t \t的NSLog(@ “无效的用户”); \t} – Rani 2011-06-17 07:07:20

相关问题