How to execute URL requests from an iOS application?

cocoa-touch, iphone, objective-c, url

Solution

For a synchronous request you would do the following:

NSURL *url = [NSURL URLWithString:@"http://www.********.com/ajax.php?script=logoutUser&username=****"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];

NSURLResponse *response;
NSError *error;
//send it synchronous
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
// check for an error. If there is a network error, you should handle it here.
if(!error)
{
    //log response
    NSLog(@"Response from server = %@", responseString);
}

Update: for doing an asynchronous request please refere to this example

Problem

I want to send the following request to the server. The server already knows what to do with it, but how can I send it? ``` http://www.********.com/ajax.php?script=logoutUser&username=**** ```

Original source