1

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

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

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

当输入正确的详细信息时,我如何仅将用户引导到新视图控制器或模态选项卡栏。

如果输入了错误的详细信息,我想显示错误。

非常感谢

登录表视图控制器.h

#import <UIKit/UIKit.h>

@interface LoginTableViewController : UITableViewController
{
UITabBarController *tbc;
}

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

@end

登录表视图控制器.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
4

1 回答 1

1

每当您提供错误的用户名或密码时,您在请求中点击的服务器端点似乎都会提供 HTML 表单。我假设它提供了不同的 HTML 页面。

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

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

于 2012-10-21T21:29:57.243 回答