-1

一段时间以来,我一直在挠头。假设我有一个这样的网址

//myurl/index.jsp?user_name=bob&user_pwd=new

以及我的应用程序上的登录表单,其中包含用户名和密码 texfields 以及登录按钮。当用户输入“bob”作为用户名并输入“new”作为密码时,我想推送到另一个视图控制器。

我知道如何使用标准的硬编码方式来做到这一点,但我想尝试通过发布到 url/web 服务。

你怎么做呢?

4

2 回答 2

0

I would use AFNetworking Library. You run the request with blocks so you can run a success block that if the login is successful, you can push a new view controller and if it fails you can throw a UIAlert or something else. You will just want to take the username and password text from the UITextFields and put that in your URL request.

More info on AFNetworking below. Also way faster than standard NSHTTP requests.

AFNetworking

于 2013-02-14T17:57:34.500 回答
0
 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
NSString *url = @"http://myurl/index.jsp?"; //your url
NSString *post = [NSString stringWithFormat:@"user_name=%@&user_pwd=%@",UserName,Password] ;
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
[request setURL:[NSURL URLWithString:url]]; 
[request setTimeoutInterval:20]; //time out interval 
request.HTTPMethod = @"POST";
[request setHTTPBody:postData];
NSError *error;
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding] autorelease];
NSLog(@"Login response %@ :",data);
[request release];
于 2013-02-14T17:45:29.277 回答