2011-06-17 43 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

相關問題