how to make login page in iphone using HTTP Post method
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
// appDelegate=(AppDelegate*)[[UIApplication sharedApplication]delegate];
// defaults=[NSUserDefaults standardUserDefaults];
usename_text.delegate=self;
password_text.delegate=self;
usename_text.layer.cornerRadius=3.0f;
usename_text.layer.masksToBounds=YES;
usename_text.layer.borderColor=[[UIColor whiteColor]CGColor];
usename_text.layer.borderWidth= 1.0f;
NSAttributedString *place_user = [[NSAttributedString alloc] initWithString:@"Email*" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
usename_text.attributedPlaceholder = place_user;
password_text.layer.cornerRadius=3.0f;
password_text.layer.masksToBounds=YES;
password_text.layer.borderColor=[[UIColor whiteColor]CGColor];
password_text.layer.borderWidth= 1.0f;
NSAttributedString *place_pwd = [[NSAttributedString alloc] initWithString:@"Password*" attributes:@{ NSForegroundColorAttributeName : [UIColor whiteColor] }];
password_text.attributedPlaceholder = place_pwd;
}
-(IBAction)Login:(id)sender
{
BOOL email_validate = [self validateEmail:usename_text.text];
BOOL pwd_validate =[self validatename:password_text.text];
if (email_validate && pwd_validate)
{
user_name_str= usename_text.text;
pwd_str= password_text.text;
[self reachability_check];
}
else
{
if (!email_validate && !pwd_validate ) {
usename_text.layer.cornerRadius=3.0f;
usename_text.layer.masksToBounds=YES;
usename_text.layer.borderColor=[[UIColor redColor]CGColor];
usename_text.layer.borderWidth= 1.0f;
password_text.layer.cornerRadius=3.0f;
password_text.layer.masksToBounds=YES;
password_text.layer.borderColor=[[UIColor redColor]CGColor];
password_text.layer.borderWidth= 1.0f;
}
}
}
#pragma mark - Validation - Email / Gender
-(BOOL)validateEmail:(NSString*)email
{
user_name_str = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", user_name_str];
return [emailTest evaluateWithObject:email];
}
-(BOOL)validatename:(NSString*)name
{
if (name.length == 0)
{
return NO;
}
else
{
return YES;
}
}
{
usename_text.layer.cornerRadius=3.0f;
usename_text.layer.masksToBounds=YES;
usename_text.layer.borderColor=[[UIColor whiteColor]CGColor];
usename_text.layer.borderWidth= 1.0f;
password_text.layer.cornerRadius=3.0f;
password_text.layer.masksToBounds=YES;
password_text.layer.borderColor=[[UIColor whiteColor]CGColor];
password_text.layer.borderWidth= 1.0f;
}
frm_user_semail and frm_user_spwd are the key type to check and post.
-(void)post_data_to_server
{
NSURL *url=[NSURL URLWithString:@“your url”];
NSString *post =[[NSString alloc] initWithFormat:@"frm_user_semail=%@&frm_user_spwd=%@",usename_text.text,password_text.text];
NSLog(@"Post is: %@",post);
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"postData is: %@",postData);
NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
NSLog(@"postLength is: %@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
//[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
if (connectionError==nil)
{
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
//json is the outut
NSString *str_name=@"";
NSString *str_id=@"";
NSString *str_usertoken=@"";
NSString *str_company=@"";
NSString *str_country=@"";
NSString *str_phone=@"";
NSString *str_errmsg=@"";
NSString *str_errcode=@"";
str_errcode = [json objectForKey:@"err_mode"];
if ([[json objectForKey:@"success"]boolValue]==YES)
{
NSMutableDictionary *u_detailsDictionary = [json objectForKey:@"u_details"];
[defaults setObject:u_detailsDictionary forKey:@"User_Details"];
[defaults synchronize];
str_name = [u_detailsDictionary objectForKey:@"name"];
str_id = [u_detailsDictionary objectForKey:@"ID"];
str_usertoken =[u_detailsDictionary objectForKey:@"user_token"];
str_company =[u_detailsDictionary objectForKey:@"company"];
str_country =[u_detailsDictionary objectForKey:@"country"];
str_phone =[u_detailsDictionary objectForKey:@"phone"];
[self gotoHomeController];
}
else if([json objectForKey:@"err_msg"])
{
str_errmsg = [json objectForKey:@"err_msg"];
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Attention!" message:str_errmsg delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
}
else
{
UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"Alert!!!" message:[[connectionError userInfo]objectForKey:@"NSLocalizedDescription"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
if(textField==usename_text)
{
[usename_text resignFirstResponder];
}
else if(textField==password_text)
{
[password_text resignFirstResponder];
}
return YES;
}
- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -0.5f * keyboardSize.height;
self.view.frame = f;
}];
}
-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}
-(void)gotoHomeController
{
ViewController *landing_vc = [[ViewController alloc]init];
[self.navigationController pushViewController:landing_vc animated:YES];
}
Comments
Post a Comment