Question

Ok, I'm having trouble finding an answer to this that isn't just use JSON or ASIHTTPRequest. But my question is a little more specific, I think. I only program as a hobby, so I'm no expert. With that said, my abstract understanding of this could be completely off base, so the question might not be answerable.

Basically, what I would like to do is send an HTTP request to a pHp script on a web server an have the web server return a value, lets say a simple integer value or bool value (think a login script that returns a yes/no). I'm not worried about security at this point as I'm trying to ease my way into understanding this.

From what I gather, I'd use something to the effect of

// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"]
                    cachePolicy:NSURLRequestUseProtocolCachePolicy
                timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
   // Create the NSMutableData to hold the received data.
   // receivedData is an instance variable declared elsewhere.
receivedData = [[NSMutableData data] retain];
} else {
// Inform the user that the connection failed.
}  

This is from the Apple documentation, and I generally understand this. So lets say it sends information through the URL to a pHp file which then:

//pHp file gets information
//pHp file uses that information to check against a mySQL database
//pHp now has the variable $result with a value of 1 in it

Also, assume I can do all of that fine. My question becomes, how do I return a value from pHp back to the iOS 'NSMutableData' variable. Do I 'echo' it out? For example:

echo "$result";

And then thats the information put into the NSMutableData (the variable $result)? I know if I were doing it strictly through pHp/HTML etc., I might use something like a session variable to allow further pages to access the variable.

Or is there some other pHp function that returns the data to NSMutabalData that I'm unaware of? Basically this is the link between the two that I'm having trouble with, the returning of data from the pHp script - I don't really understand what it is that NSMutableData is 'getting'.

I've tried to be as detailed as possible, so sorry for the long question. Please just assume this is all I'm trying to do, return a value. Security isn't important (I'm trying to do this at the most simple level to understand it then build on it). I'm trying to avoid any vague answers such as 'write a web service' or 'use _' to do it, and I'm sure there are lots of better ways to do it, but I'm not rushing out a program - I'm just trying to learn this for a better understanding of NSURLConnection, pHp, NSMUtableData and how al the cogs fit together. If what I'm asking is technically impossible and doesn't make sense, I appreciate that answer too. I've tried searching and haven't found a satisfactory answer for this single piece of the puzzle.

Thanks in advance.

EDIT:

I've tried

...
NSMutableData *receivedData = [[NSMutableData alloc] init];
receivedData = [[NSMutableData data] retain];
NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
label.text = receivedDataString; 
...

Which calls a pHp file on the server that is basically:

<?pHp
$result = "TRUE";
var_dump($result);
?>

Which doesn't work. I think my problem is on the pHp side, is there some special command that returns a value to the NSMutableData object? (Just some extra info to maybe clarify my question more).

EDIT II:

I can't seem to respond directly, probably because I've only ever used the site anonymously before and I have a brand new account :) Thanks for your answer Charles, I've tried to use echo and var_dump, neither seem to send the data back (my NSMutableData object when converted to an NSString seems to be empty - unless my objective C is wrong, but I've parsed it down to pretty much the bare minimum so I don't believe that's the case).

Was it helpful?

Solution

Ok, I believe I got it to work and so I'm going to post an answer in the event anyone else is looking for the same.

The PHP file on the server is as follows:

<?php
echo 'Works';
?>

My h file contains

#import <UIKit/UIKit.h>

@interface SampleViewController : UIViewController {
IBOutlet UIButton *theButton;
NSMutableData *receivedData;}
@property (nonatomic, retain) IBOutlet UIButton *theButton;
-(IBAction) clickTheButton:(id)sender;
@end

My m file contains in relevant part (everything else is just the standard stuff Apple puts into an m file when you create it in SDK

-(IBAction) clickTheButton:(id)sender {
NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"<path to my php file>try.php"]
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
    receivedData = [[NSMutableData data] retain];
    NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
    NSLog(@"This1: %@", receivedData);
    NSLog(@"This2: %@", receivedDataString);
} else {
    // Inform the user that the connection failed.
}
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
[receivedData setLength:0];
NSLog(@"This3: %@", receivedData);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
// Append the new data to receivedData.
// receivedData is an instance variable declared elsewhere.
[receivedData appendData:data];
NSString *receivedDataString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];
NSLog(@"This4: %@", receivedDataString);
NSLog(@"This5: %@", receivedData);
}

The NSLogs output as follows:

This1: '<>'

This2:

This3: '<>'

This4: Works

This5: '<24726573 756c74>'

To answer my original question, echo 'Works'; is the PHP code to return the word Works to the NSMutableData object. After setting up the connection, I needed to call the didRecieveData: method to actually put the echoed information into the data file.

OTHER TIPS

Basically this is the link between the two that I'm having trouble with, the returning of data from the pHp script - I don't really understand what it is that NSMutableData is 'getting'.

If it works like every other thing that looks like it's just making an HTTP request, you will either get a string containing the output from the requested URL, or you will get an object from which you can get that output (including headers, etc). I know nothing about Objective C or iOS, but I'm going to wager that you'll just be getting the output of the script back.

In other words,

how do I return a value from pHp back to the iOS 'NSMutableData' variable. Do I 'echo' it out?

Probably!

Why don't you try out the code and see what you get? If it works, then congratulations: you've created your first web service.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top