2012-10-21 60 views
1

我有一個登錄頁面,它向URL發出請求並將變量或用戶名和密碼發佈到URL,然後在完成後將我帶到新的View Controller。爲HTTP請求添加錯誤和成功處理程序

當我把正確的用戶名和密碼的請求工作,因爲我可以複製在日誌中的HTML它顯示我我的個人資料,我提出了要求。

如果我把錯誤的細節和我複製的HTML,我從日誌或控制檯獲得登錄頁面的HTML。

我怎麼會只將用戶引導至新的View控制器或模態的TabBar當正確的細節投入。

我想表明一個錯誤,如果輸入了錯誤的詳細信息。

非常感謝

LoginTableViewController.h

#import <UIKit/UIKit.h> 

@interface LoginTableViewController : UITableViewController 
{ 
UITabBarController *tbc; 
} 

- (void)dismissTabBar; 
@property (nonatomic, retain) UITabBarController *tbc; 

@end 

LoginTableViewController.m

#import "LoginTableViewController.h" 
#import "rootViewController.h" 

@interface LoginTableViewController() 
@property (weak, nonatomic) IBOutlet UITextField *UIEmailTextField; 
@property (weak, nonatomic) IBOutlet UITextField *UIPasswordTextField; 

@end 

@implementation LoginTableViewController 
@synthesize UIEmailTextField; 
@synthesize UIPasswordTextField; 
@synthesize tbc; 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
self = [super initWithStyle:style]; 
if (self) { 
    // Custom initialization 
} 
return self; 
} 

- (void)viewDidLoad 
{ 


[super viewDidLoad]; 
} 

- (void)viewDidUnload 
{ 
[self setUIEmailTextField:nil]; 
[self setUIPasswordTextField:nil]; 
[super viewDidUnload]; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - barButton Outlet 

- (IBAction)loginButtonPressed:(UIBarButtonItem *)sender { 
NSString *data = [NSString stringWithFormat:@"username=%@&password=%@",UIEmailTextField.text, UIPasswordTextField.text]; 
NSData *postData = [data dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

// preaparing URL request to send data. 

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ; 
NSString *url = [NSString stringWithFormat:@"https://online.vrmcapital.co.za"]; 
[request setURL:[NSURL URLWithString:url]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:postData]; 
[request setTimeoutInterval:7.0]; 

NSURLResponse *response; 
NSError *error; 

NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 

NSLog(@"Login response:%@",str); 

NSLog(@"Log In button was pressed!"); 


NSLog(@"Tab Bar Controller Button Clicked"); 
UIViewController *blueController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; 
blueController.view.backgroundColor = [UIColor blueColor]; 
blueController.title = @"Blue"; 
blueController.tabBarItem.image = [UIImage imageNamed:@"Gallery.png"]; 

UIViewController *redController = [[UIViewController alloc] initWithNibName:nil bundle:nil]; 
redController.view.backgroundColor = [UIColor redColor]; 

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button setFrame:CGRectMake(20.0f, 140.0f, 280.0f, 40.0f)]; 
[button setTitle:@"Done" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(dismissTabBar) forControlEvents:UIControlEventTouchUpInside]; 

[redController.view addSubview:button]; 
//redController.title = @"Red1"; 
[redController setTitle:@"Red1" ]; 
redController.tabBarItem.image = [UIImage imageNamed:@"Gallery.png"]; 
image:[UIImage imageNamed:@"Gallery.png"]; 


tbc = [[UITabBarController alloc] initWithNibName:nil bundle:nil]; 
tbc.viewControllers = [NSArray arrayWithObjects:blueController, redController, nil]; 
tbc.selectedViewController = redController; 
NSLog(@"Selected index = %d of %d", tbc.selectedIndex, [tbc.viewControllers count]); 

//[blueController release]; 
//[redController release]; 
[self presentViewController:tbc animated:YES completion:nil]; 



} 

- (void)dismissTabBar { 
[[self tbc] dismissViewControllerAnimated:YES completion:nil]; 

} 

@end 

回答

1

看來,你在你的要求擊中服務器端點提供了一個HTML只要您提供錯誤的用戶名或密碼,就會形成表格密碼。我認爲它提供了一個不同的HTML頁面,否則。

無論哪種方式,沒有一個適當的網絡服務,你將不得不處理HTML解析。您希望Web開發人員不會決定以破壞解析器的方式更改頁面......再次,如果沒有適當的Web服務,您將不得不依賴HTML頁面解析來區分成功和不成功的請求。

另一方面,你真的想在主線程上使用NSURLConnection的同步API嗎?如果網絡連接不好,您的主線程將被阻止。