可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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]; } }