using HTTP Post method how to make login page in iphone

匿名 (未验证) 提交于 2019-12-03 03:04:01

问题:

I'm have created a Login view. Everytime I login it gives me login Success message will be displayed. even if I enter wrong username and password.I am created login page static.The menctioned link is sample web services link. This is the method I'm using right now:Please give me any idea.Thanks in advance.

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]);  }  } 

回答1:

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];         } 

Don't need to use JSON you can do this without JSON in a esay way!!!



回答2:

- (void) alertStatus:(NSString *)msg :(NSString *)title {     UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title                                                         message:msg                                                        delegate:self                                               cancelButtonTitle:@"Ok"                                               otherButtonTitles:nil, nil];      [alertView show];  }   - (IBAction)loginClicked:(id)sender {     @try {          if([[txtUserName text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {             [self alertStatus:@"Пожалуйста заполните все поля!!!" :@"Авторизация не удолась!"];         } else {              NSString *post =[[NSString alloc] initWithFormat:@"login=%@&pass=%@",[txtUserName text],[txtPassword text]];               NSURL *url=[NSURL URLWithString:@"http:xxxxxxxx.xxx/?"];              NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];              NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];              NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];             [request setURL:url];             [request setHTTPMethod:@"POST"];             [request setValue:postLength forHTTPHeaderField:@"Content-Length"];             [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];             [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];             [request setHTTPBody:postData];              NSError *error = [[NSError alloc] init];             NSHTTPURLResponse *response = nil;             NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];                 if ([response statusCode] >=200 && [response statusCode] <300)             {                 NSData *responseData = [[NSData alloc]initWithData:urlData];                 NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];                   if([jsonObject objectForKey:@"error"])                 {                     [self alertStatus:@"" :@""];                  } else {                      [self alertStatus:@"" :@""];                 }              } else {                 if (error) NSLog(@"Error: %@", error);                 [self alertStatus:@"Connection Failed" :@"Login Failed!"];             }         }     }     @catch (NSException * e) {         NSLog(@"Exception: %@", e);         [self alertStatus:@"Login Failed." :@"Login Failed!"];     }      [txtUserName resignFirstResponder];     [txtPassword resignFirstResponder]; } 


回答3:

Try below code.

-(void)webservice_Call { NSString *urlString=@"http://api.openweathermap.org/data/2.1/find/city?lat=10.369&lon=122.5896";  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]                                                        cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData                                                    timeoutInterval:10];  [request setHTTPMethod: @"GET"];  NSError *requestError; NSURLResponse *urlResponse = nil;   NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];  NSDictionary *resDictionary = [NSJSONSerialization JSONObjectWithData:response1 options:NSJSONReadingMutableContainers error:Nil];  } 


回答4:

Post Method for iOS 9 Version



回答5:

-(void)apiCode {     NSString *string= [NSString stringWithFormat:@"http:url...project_id=1&user_id=58&question=%@&send_enquiry=%@",self.txtTitle.text,self.txtQuestion.text];     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 = @"success";     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];     } } 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!