Dion
Dion

Reputation: 3335

how to fetch page content of an URL Request (post)

I have a little Mac application which should be able to post Data to my web server which saves the data in a database. Now that's the Code I have now:

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

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

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:@"http://..."]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    NSURLConnection * connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (connection) NSLog(@"Done");

And it works fine. But now I want to check whether the data was correct (and stored in the database) or something (like eMail) is wrong. The PHP file prints e.g. "email incorrect" out if the E-Mail is not correct. But how can I fetch this data (which PHP prints out) in Xcode that the App knows whether it was successful or not?

Thanks for answers!

Upvotes: 1

Views: 731

Answers (2)

jonkroll
jonkroll

Reputation: 15722

You need to implement the NSURLConnectionDelegate methods connection:didReceiveData: and connectionDidFinishLoading:

According to the docs didReceiveData: may be called multiple times per NSURLRequest (i.e. the response will not always arrive all at once) so the recommended method is to append the incoming data to buffer during connection:didReceiveData: and then do any processing on the data in connectionDidFinishLoading:.

You could create a property on your class like this:

@property (nonatomic, strong) NSMutableData *dataBuffer;

And instantiate your buffer during viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.dataBuffer = [[NSMutableData alloc] init];

    // do any other setup your class requires...    
}

And then implement the delegate methods:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    // clear the buffer in case it has been used previously
    [self.dataBuffer setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [self.dataBuffer appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
    NSString *response = [NSString stringWithUTF8String:[self.dataBuffer bytes]]); 
    NSLog(@"response from HTTP request=>%@", response);
}

This can all also be done using a third-party networking library like ASIHTTPRequest (which is no longer under active develoment) or AFNetworking, but sometimes those can be overkill depending upon what you are trying to accomplish

Upvotes: 3

Kiran Panesar
Kiran Panesar

Reputation: 3204

Implement the delegate method for the NSURLConnection,

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response. This method is called by the app when the request finishes. You can access response data using the 'response' parameter.
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;

Then just convert the 'data' parameter into a string using:

NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

Then you can search the response for whatever string you want, e.g., "email incorrect".

PS: I generally don't use NSURLConnection/NSURLRequest for HTTP requests, I'd recommend you check out ASIHTTPRequest for really simple HTTP requests/connections.

Hope this helps.

Upvotes: 2

Related Questions