我已经创建了一个登录 View 。每次我登录时都会显示登录成功消息。即使我输入了错误的用户名和密码。我创建的登录页面是静态的。提到的链接是示例 Web 服务链接。这是我现在正在使用的方法:请给我任何想法。提前致谢。
loginPage.m
-
(IBAction)login:(id)sender
{
NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"HTTP://URL"]]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if (conn)
{
NSLog(@"connection successful");
}
else
{
NSLog(@"Failed");
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
}
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[receivedData setLength:0];
// NSURL *theURL=[response URL];
}
-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
if(receivedData)
{
NSLog(@"success",[receivedData length]);
}
else
{
NSLog(@"Success",[receivedData length]);
}
}
最佳答案
NSString *string= [NSString stringWithFormat:@"your Url.php?&Username=%@&Password=%@",username,password];
NSLog(@"%@",string);
NSURL *url = [NSURL URLWithString:string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
NSString *str = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"responseData: %@", str);
NSString *str1 = @"1";
if ([str isEqualToString:str1 ])
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Successfully" message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
else
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Try Again" message:@"" delegate:self cancelButtonTitle:@"Try Later" otherButtonTitles:@"Call", nil];
alert.tag = 1;
[alert show];
}
不需要使用 JSON 你可以在没有 JSON 的情况下以简单的方式做到这一点!!!
关于iphone - 使用 HTTP Post 方法如何在 iphone 中制作登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18573323/