这是点击登录按钮后的功能(使用http请求/响应执行用户身份验证)
(IBAction)loginbutton:(id)sender {
@try {
if([[userid text] isEqualToString:@""] || [[pass text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
} else {
NSString *GET =[[NSString alloc] initWithFormat:@"userid=%@&pass=%@",[userid text],[pass text]];
NSLog(@"getData: %@",GET);
NSURL *url=[NSURL URLWithString:@"192.168.1.14:8888/itrack/mobile/login.php"];
NSData *getData;
NSString *getLength = [NSString stringWithFormat:@"%@,%@" ,@"userid" ,@"pass"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"GET"];
[request setValue:getLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:getData];
//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];
NSError *error = nil;
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"Response code: %ld", (long)[response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);
SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"success"] integerValue];
NSLog(@"%ld",(long)success);
if(success == 1)
{
NSLog(@"Login SUCCESS");
[self alertStatus:@"Logged in Successfully." :@"Login Success!"];
} else {
NSString *error_msg = (NSString *) [jsonData objectForKey:@"error_message"];
[self alertStatus:error_msg :@"Login Failed!"];
}
} else {
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Login Failed!"];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Login Failed." :@"Login Failed!"];
}
即使我输入了正确的ID和密码,它仍然显示连接失败。不知道怎么解决这个问题? 非常感谢!