2016-08-13 15 views
0

首先,我有兩個文本字段,第一個是登錄,第二個是密碼和一個登錄按鈕。我正在使用通過push segue連接到另一個視圖控制器的故事板和登錄按鈕。 這次在我的項目中工作,將用戶名和密碼放入文本字​​段,並在移動另一個視圖後選擇登錄按鈕。我輸入了錯誤的密碼登錄仍然是移動另一個視圖。我認爲某個地方的情況是錯誤的。 請幫助我到底該怎麼做。謝謝如何成功登錄並使用php web服務移動到另一個視圖

我按照這個教程Dipin Krishna Tutorial

我的代碼是

- (IBAction)loginac:(id)sender { 
    NSInteger success = 0; 
    @try { 

     if([[self.txfld text] isEqualToString:@""] || [[self.tx1 text] isEqualToString:@""]) { 

      [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0]; 

     } else { 
      NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.txfld text],[self.tx1 text]]; 
      NSLog(@"PostData: %@",post); 

      NSURL *url=[NSURL URLWithString:@"https://dipinkrishna.com/jsonlogin.php"]; 

      NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

      NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

      NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
      [request setURL:url]; 
      [request setHTTPMethod:@"POST"]; 
      [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
      [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
      [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
      [request setHTTPBody:postData]; 

      //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

      NSError *error = [[NSError alloc] init]; 
      NSHTTPURLResponse *response = nil; 
      NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

      NSLog(@"Response code: %ld", (long)[response statusCode]); 

      if ([response statusCode] >= 200 && [response statusCode] < 300) 
      { 
       NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
       NSLog(@"Response ==> %@", responseData); 

       NSError *error = nil; 
       NSDictionary *jsonData = [NSJSONSerialization 
              JSONObjectWithData:urlData 
              options:NSJSONReadingMutableContainers 
              error:&error]; 

       success = [jsonData[@"success"] integerValue]; 
       NSLog(@"Success: %ld",(long)success); 

       if(success == 1) 
       { 
        NSLog(@"Login SUCCESS"); 
       } else { 

        NSString *error_msg = (NSString *) jsonData[@"error_message"]; 
        [self alertStatus:error_msg :@"Sign in Failed!" :0]; 
       } 

      } else { 
       //if (error) NSLog(@"Error: %@", error); 
       [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0]; 
      } 
     } 
    } 
    @catch (NSException * e) { 
     NSLog(@"Exception: %@", e); 
     [self alertStatus:@"Sign in Failed." :@"Error!" :0]; 
    } 

    if (success) { 
     [self performSegueWithIdentifier:@"login_success" sender:self]; 
    } 
} 

- (void) alertStatus:(NSString *)msg :(NSString *)title :(int) tag 
{ 
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title 
                 message:msg 
                 delegate:self 
               cancelButtonTitle:@"Ok" 
               otherButtonTitles:nil, nil]; 
    alertView.tag = tag; 
    [alertView show]; 
} 
+0

你在corect方式需要一個小的修改 –

+0

@ Anbu.Karthik感謝重播,哪裏需要一個小的修改 –

+0

我張貼的答案檢查一次 –

回答

2

刪除此

if (success) { 
[self performSegueWithIdentifier:@"login_success" sender:self]; 

} 

,並添加到SUCCESS條件裏面,肯定工作

if(success == 1) 
     { 
      NSLog(@"Login SUCCESS"); 
      [self performSegueWithIdentifier:@"login_success" sender:self]; 
     } 

爲完整的答案

- (IBAction)loginac:(id)sender { 


    if([[self.txfld text] isEqualToString:@""] || [[self.tx1 text] isEqualToString:@""]) { 

     [self alertStatus:@"Please enter Email and Password" :@"Sign in Failed!" :0]; 

    } else { 
     NSString *post =[[NSString alloc] initWithFormat:@"username=%@&password=%@",[self.txfld text],[self.tx1 text]]; 
     NSLog(@"PostData: %@",post); 

     NSURL *url=[NSURL URLWithString:@"https://dipinkrishna.com/jsonlogin.php"]; 

     NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

     NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
     [request setURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 

     //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

     NSError *error = [[NSError alloc] init]; 
     NSHTTPURLResponse *response = nil; 
     NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 

     NSLog(@"Response code: %ld", (long)[response statusCode]); 

     if ([response statusCode] >= 200 && [response statusCode] < 300) 
     { 


      NSError *error = nil; 
      NSDictionary *jsonData = [NSJSONSerialization 
             JSONObjectWithData:urlData 
             options:NSJSONReadingMutableContainers 
             error:&error]; 

      int success = [jsonData[@"success"] integerValue]; 


      if(success == 1) 
      { 
       NSLog(@"Login SUCCESS"); 
       [self performSegueWithIdentifier:@"login_success" sender:self]; 
      } else { 

       NSString *error_msg = (NSString *) jsonData[@"error_message"]; 
       [self alertStatus:error_msg :@"Sign in Failed!" :0]; 
      } 

     } else { 
      //if (error) NSLog(@"Error: %@", error); 
      [self alertStatus:@"Connection Failed" :@"Sign in Failed!" :0]; 
     } 
    } 



} 
+0

我按照你的條件但我輸入了錯誤的密碼登錄仍然是,並移動另一個視圖。 –

+0

同樣的問題創造它如何可能。 –

+0

我正在使用導航控制器 –

相關問題