为HTTP请求添加错误和成功处理程序

时间:2012-10-21 18:09:54

标签: objective-c ios cocoa-touch asihttprequest

我有一个登录页面,它向URL发出请求并将变量或用户名和密码发布到URL,然后在完成后将我带到新的View Controller。

当我输入正确的用户名和密码时,请求有效,因为我可以复制日志中的html,它会向我显示我发送请求的个人资料。

如果我输入错误的详细信息并复制html,我会从日志或控制台获取登录页面html。

如果输入了正确的详细信息,我将如何仅将用户定向到New View Controller或Modal 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 个答案:

答案 0 :(得分:1)

当您提供错误的用户名或密码时,您在请求中遇到的服务器端点似乎会提供HTML表单。我认为它提供了另一个不同的HTML页面。

无论哪种方式,如果没有适当的Web服务,您将不得不处理HTML解析。您希望Web开发人员不会决定以破坏解析器的方式更改页面...再次,如果没有正确的Web服务,您将不得不依赖HTML页面解析来区分成功和不成功的请求。

顺便说一下,你真的想在主线程上使用NSURLConnection的同步API吗?如果网络连接不佳,主线程将被阻止。