2012-10-21 99 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吗?如果网络连接不好,您的主线程将被阻止。