我是ios新手。我正在创建登录页面。在登录页面中有两个字段usernameTxt
和passwordTxt
. 当我点击logonBtn
按钮时,我们必须检查用户名和密码。我们必须检查网络服务。我正在使用 JSON 解析器。
请帮助我如何编码此页面。
我正在使用以下代码
- (void) alertStatus:(NSString *)msg :(NSString *)title
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
message:msg
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alertView show];
}
-(IBAction)LoginBtnClick:(id)sender
{
greeting.text= @"";
NSString* username = usernameTxt.text;
NSString* pass = passwordTxt.text;
greeting.hidden = NO;
if([usernameTxt.text isEqualToString:@"" ] && [passwordTxt.text isEqualToString:@""])
{
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
greeting.text = @"Please enter username and password.";
[usernameTxt resignFirstResponder];
[passwordTxt resignFirstResponder];
return;
}
if([usernameTxt.text isEqualToString:@"" ])
{
[self alertStatus:@"Please enter Username" :@"Login Failed!"];
greeting.text = @"Please enter username.";
[usernameTxt resignFirstResponder];
[passwordTxt resignFirstResponder];
return;
}
if([passwordTxt.text isEqualToString:@""])
{
[self alertStatus:@"Please enter Password" :@"Login Failed!"];
greeting.text = @"Please enter password.";
[usernameTxt resignFirstResponder];
[passwordTxt resignFirstResponder];
return;
}
// NSDictionary* params = @{@"username": @"mahedra.z@homework.do", @"password": @"admin123$"};
NSString *post = [[NSString alloc] initWithFormat:@"uname=%@&pwd=%@",username,pass];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSURL *url = [NSURL URLWithString:@"http://192.168.10.28:5000/wsserver.php"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:postData];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
// indicator.hidden = NO;
webData = [[NSMutableData alloc]init];
}
else
{
}
[usernameTxt resignFirstResponder];
[passwordTxt resignFirstResponder];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webData setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self alertStatus:@"Connection Error" :@"Login Failed!"];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary* params = @{@"username": @"mahedra.z@homework.do", @"password": @"admin123$"};
NSString *respstring = [[NSString alloc]initWithData:webData encoding:NSUTF8StringEncoding];
SBJsonParser *objSBJsonParser = [[SBJsonParser alloc]init];
NSDictionary *respDict = [[NSDictionary alloc]initWithDictionary:[objSBJsonParser objectWithString:respstring]];
NSLog(@"respDict: %@",respDict);
// NSDictionary *params = [[NSDictionary alloc]initWithDictionary:[respDict valueForKey:@"userDetails"]];
NSLog(@"user details: %@",params);
NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
greeting.text = loginStatus;
[[self navigationController]pushViewController:snsViewController animated:YES];
// indicator.hidden = YES;
}