0

I've successfully sent a POST request to my server but I want to know how to send a response from PHP without using JSON objects or the database. I was thinking I could always send the database row values for the particular entry to my application and then compare them to see if the values match, but that seems a little verbose and undesirable. I'd much rather send an HTTP POST response from my PHP script, if the log is correct which would contain a boolean variable depending on whether the encrypted passwords matched. I've looked all over the place but I can only find information about sending HTTP POST requests from the application, not receiving them from a PHP script on a server.

Edit: Here is an IBAction which I'm using to open the connection to send data with POST.

NSString *registrationPOSTMessage = [NSString stringWithFormat:@"username=%@&password=%@&emailAddress=%@",self.registrationFieldUsername.text,self.registrationFieldPassword.text,self.registrationFieldEmail.text];
NSData *ASCIIEncodedRegistrationPOSTData = [registrationPOSTMessage dataUsingEncoding:NSASCIIStringEncoding];
NSString *ASCIIEncodedRegistrationPOSTDataLength = [NSString stringWithFormat:@"%ul",(unsigned)[ASCIIEncodedRegistrationPOSTData length]];
NSMutableURLRequest *registrationRequest = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.hausofalexander.tk/someiOSApplication/Register.php"]];
[registrationRequest setHTTPMethod:@"POST"];
[registrationRequest setHTTPBody:ASCIIEncodedRegistrationPOSTData];
[registrationRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[registrationRequest setValue:ASCIIEncodedRegistrationPOSTDataLength forHTTPHeaderField:@"Content-Length"];
NSURLConnection *registrationConnection = [[NSURLConnection alloc] initWithRequest:registrationRequest delegate:self];
if (registrationConnection) {
    NSLog(@"The registration information has been sent! :D");
} else if (!registrationConnection) {
    NSLog(@"For some reason the registration information couldn't be sent. :(");
} else {
    NSLog(@"Something horrible has happened :|");
}
Himanshu Joshi
  • 3,391
  • 1
  • 19
  • 32
IIllIIll
  • 504
  • 8
  • 25
  • Why do you need to receiving them from php script? If you want to get something from your database, you should send a request and it will response the data for you – Ryan Wu Dec 18 '14 at 04:49
  • That's exactly what I need. I want to know how I can send an HTTP response from the PHP script to my iOS application in order to verify that the passwords matched and the user has been granted permission to use their account from the application. How could I do that? I was thinking I may be able to use cookies somehow or send another response from my PHP script with some POST data which I could compare against in my iOS application. – IIllIIll Dec 18 '14 at 05:10
  • The way you send a request from iOS should provide a callback or delegate method to let you handle the response data. If you're using the JSON format, you can simply echo the data from your php like this http://stackoverflow.com/a/4064468/471840. – Ryan Wu Dec 18 '14 at 05:16
  • Thanks. I've read something about a method you can call on an NSURLConnection object called 'sendAsynchronousRequest:queue:completionHandler:', but when I try calling that on my instance of NSURLConnection it simply doesn't show and I don't understand what I should pass to it anyway. I'm going to add an edit with my code. Tell me exactly which callback or delegate method I should be looking for and how I can handle this. Thanks! – IIllIIll Dec 18 '14 at 05:27
  • I will recommend you use https://github.com/AFNetworking/AFNetworking, its a popular library which provides easy-to-use methods to handle the HTTP request and response, I can send you an example if you want. – Ryan Wu Dec 18 '14 at 06:47
  • Thanks, but I'd really like to keep it down to the most vanilla possible at the moment. I'll consider libraries later! – IIllIIll Dec 18 '14 at 07:04
  • Great, feel free to ask any question. – Ryan Wu Dec 18 '14 at 07:07

2 Answers2

0

If you want to use the delegate way (you're currently using), make sure your class implement the delegate

@interface MyViewController : UIViewController <NSURLConnectionDataDelegate>

and you can use following delegate method to get the data

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"Did Receive Response %@", response);
}
- (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
{
    NSLog(@"Did Receive Data %@", data);
}

Another example is using the sendAsynchronousRequest method, simply replace your registrationConnection

[NSURLConnection sendAsynchronousRequest:registrationRequest queue:[[NSOperationQueue alloc] init]
                 completionHandler:^(NSURLResponse* response, NSData* data, NSError* connectionError) {
                            NSLog(@"Response %@", response);
                            NSLog(@"Data %@", data); //Add a breakpoint here to find out whats the  
                           }];
Ryan Wu
  • 5,963
  • 2
  • 36
  • 47
  • 1
    Honestly I'm still a bit of an iOS development noob but I'm definitely going to try this as soon as I have the chance. Hopefully I won't run into an issue! Thank you so much for sharing. – IIllIIll Dec 18 '14 at 10:02
0

I've figured out a way to return a value from an associative array in my PHP script. I was basically asking if there were a better way to receive this data (id est by sending an HTTP POST request from my PHP script to my application which would then store the value of that POST request as a variable in the application). This is what I've done to return information from the PHP request to the application. It uses JSON which I wanted to avoid, but I'm guessing this is the only (or at least, the mosts beginner-friendly way)

In my iOS application I've added a button to the storyboard which will trigger an IBAction which will send a POST request to my PHP document which I have on my server. This line is near the end of that PHP document: echo json_encode(array('registrationSuccessful'=>'true')), which creates an associative array then converts it to JSON syntax and returns that to the requesting HTTP client (my iOS application). That means I can use NSJSONSerialization to convert the returned JSON data to an NSArray and use the values of that NSArray within the application to do (or not to do) other things in the application.

I feel like this won't make much sense, so ask a question if you'd like a more in-depth explanation. I'd be happy to provide one not only for other guests but also for myself because I'm more than likely going to forget how to do this in a few months. :(

IIllIIll
  • 504
  • 8
  • 25