2015-06-22 59 views
0

我想測試用戶完成所有字段後連接設置是否正確。我已經連線,但如果用戶名或密碼不正確,我想要一個條件。使用xcode測試ios應用程序的登錄

我MainViewController.m

NSString* host = @"http://www.example.com"; 
     NSString* complement = @"/account/login"; 



     NSString* user_name = [[NSUserDefaults standardUserDefaults] valueForKey:@"Identifiant"]; 
     NSString* password = [[NSUserDefaults standardUserDefaults] valueForKey:@"Password"]; 
     //Add URL + Folder 
     NSString *urlString = [NSString stringWithFormat:@"%@%@", host, complement]; 


     //NSURL *url = [NSURL URLWithString:urlString]; 
     NSURL *url = [NSURL URLWithString:urlString]; 


     //NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

     //Add some informations POST 
     NSString *post = [NSString stringWithFormat:@"user_name=%@&password=%@&submit=submit", user_name, password];; 


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

     NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]]; 
     NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
     [request setHTTPMethod:@"POST"]; 
     [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
     [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
     [request setHTTPBody:postData]; 


     NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 



     [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
     { 
      if ([data length] > 0 && error == nil) [_loadscreen loadRequest:request]; 
      else if (error != nil) NSLog(@"Error: %@", error); 
     }]; 

如果密碼或ID是不正確的,我想顯示一個錯誤,其實,當它是不正確,它是在網頁視圖顯示登錄頁面,但我不要顯示登錄網頁。

+0

的意見嗎? – Mrunal

+0

目前沒有顯示,沒有錯誤,沒有迴應。我覺得應用程序收到一個積極的響應,突然webview加載,但一旦加載它將我重定向到登錄頁面,因爲標識符不正確 – ewan

回答

1

爲什麼你這樣做,爲什麼你使用'POST'方法你沒有發佈到服務器頁面的東西。你只需要實現Json解析多數民衆贊成它試試這個:

你只需要在你的PHP代碼結束做一些更正。當userId &密碼不匹配時,告訴他們返回成功=「0」,如果它們匹配,則返回「1」。 :

NSHTTPURLResponse *response = nil; 
NSString *jsonUrlString = [NSString stringWithFormat:@"http://www.example.com/pagename.php?username=%@&password=%@",emailText.Text,passwordText.Text]; 

NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url]; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil]; 
NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; 

NSLog(@"%@",result); 

NSString *stringData = [result valueForKey:@"success"]; 
if ([stringData isEqualToString:@"0"]) { 
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:nil message:@"wrong credentials" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; 
[alert show]; 
} else { 
//perform anything you want to perform 
} 
+0

我需要在我的PHP文件上寫一個json代碼? – ewan

+1

不,不,這段代碼是用XCode編寫的。當您填寫用戶名和密碼後按下提交按鈕。將該代碼粘貼到該函數下。並要求你php的人在PHP結束時採取汽車。 – Nitin

+0

當我嘗試的代碼,我有這個日誌:(空),這是錯誤日誌,我沒有登錄 – ewan

0

入住你在響應和錯誤獲取塊

[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
     { 
      if ([data length] > 0 && error == nil) 
      { 
       [_loadscreen loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:@"<new page url>"]]]; 
       // Over here you are loading the same request object, which contains older request URL in it. 
       // There should be new URL request which lead the user to next page URL. 
      } 
      else if (error != nil) 
      { 
       NSLog(@"Error: %@", error); 
      } 
     }]; 
+0

如何加載不同的URL? – ewan

+0

更新回答。 – Mrunal

+0

我總是重定向到我的登錄頁面:/ – ewan