我想測試用戶完成所有字段後連接設置是否正確。我已經連線,但如果用戶名或密碼不正確,我想要一個條件。使用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是不正確的,我想顯示一個錯誤,其實,當它是不正確,它是在網頁視圖顯示登錄頁面,但我不要顯示登錄網頁。
的意見嗎? – Mrunal
目前沒有顯示,沒有錯誤,沒有迴應。我覺得應用程序收到一個積極的響應,突然webview加載,但一旦加載它將我重定向到登錄頁面,因爲標識符不正確 – ewan