Send a parameter to PHP and wait for a response

I am creating a login system for mobile applications and you need to send the username and password using the POST / GET method to the PHP project.

Well, I read a few manuals on the Internet and saw that most of them teach how to do this, but I need to send a message and get the values ​​that are generated via PHP, that is:

  • We send username and password for PHP
  • If the username is incorrect, PHP displays the wrong username and password.
  • If you are correct, another message is displayed on the screen.

And that what I want to do is no longer send the parameter, I want to get the answer from the PHP file, is this possible in Ios?

+1
ios objective-c iphone
source share
3 answers

The following code describes a simple example using the POST method. (How to transfer data using the POST method)

You can use the following code snippet as described in this article :

Here I just describe how to use the POST method.

1. Set a message string with the actual username and password.

 NSString *post = [NSString stringWithFormat:@"&Username=%@&Password=%@",@"username",@"password"]; 

2. Encode the message string using NSASCIIStringEncoding , as well as the message string that must be sent in NSData format.

 NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

You need to send the actual length of your data. Calculate the line length of a message.

 NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 

3. Create an Urlrequest with all properties of type HTTP , an HTTP header field with a message line length. Create a URLRequest object and initialize it.

 NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; 

Set the Url for which you are going to send data to this request.

 [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.abcde.com/xyz/login.aspx"]]]; 

Now set the HTTP method (POST or GET). Write these lines as in your code.

 [request setHTTPMethod:@"POST"]; 

Set an HTTP header field with the message data length.

 [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

Also set the encoded value for the HTTP header.

 [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; 

Set HTTPBody urlrequest with postData.

 [request setHTTPBody:postData]; 

4. Now create the URLConnection object. Initialize it using URLRequest.

 NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

It returns an initialized URL connection and starts loading data for the url request. You can check if the URL you connected is running correctly or if the if / else statement is not used, as shown below.

 if(conn) { NSLog(@"Connection Successful") } else { NSLog(@"Connection could not be made"); } 

5 .. To get data from an HTTP request, you can use the delegate methods provided by the URLConnection class reference. The delegation methods are as follows.

 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data 

The above method is used to obtain the data that we receive using the Method message.

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 

This method, which you can use to get an error report in case of connection to the server, is not performed.

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 

The above method is used to process the data after the connection is successful.

Also see This and This is the documentation for the POST method.

And here is the best source code example of the HTTPPost method.

+3
source share
 //Create the request NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"your script name.php"]]; // create the Method "GET" or "POST" [request setHTTPMethod:@"POST"]; //Pass The String to server NSString *userUpdate =[NSString stringWithFormat:@"artist_email=%@ ",your string Name,nil]; //Check The Value what we passed NSLog(@"the data Details is =%@", userUpdate); //Convert the String to Data NSData *data1 = [userUpdate dataUsingEncoding:NSUTF8StringEncoding]; //Apply the data to the body [request setHTTPBody:data1]; //Create the response and Error NSError *err; NSURLResponse *response; NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err]; NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding]; //This is for Response NSLog(@"got response==%@", resSrt); if(resSrt) { NSLog(@"got response"); } else { NSLog(@"faield to connect"); } 
+3
source share

Processing Result Data Using NSURLConnection Delegation Methods

  NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",@"Raja",@"12345"]; NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[post length]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/promos/index.php"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setHTTPBody:postData]; NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:request delegate:self]; if( theConnection ){ // indicator.hidden = NO; mutableData = [[NSMutableData alloc]init]; } 

your php code

 <?php $username = $_POST['username']; $password = $_POST['password']; $result=//check your condition echo $result; ?> 
+2
source share

All Articles