Using ASIHTTPRequest

| Comments

while reading a tutorial found this great framework, ASIHTTPRequest, it’s so damn cool and really easy to use, so i decide to explain how you can use it in your own iOS project.

What is ASIHTTPRequest?

ASIHTTPRequest is an easy to use wrapper around the CFNetwork API that makes some of the more tedious aspects of communicating with web servers easier. It is written in Objective-C and works in both Mac OS X and iPhone applications.

It is suitable performing basic HTTP requests and interacting with REST-based services (GET / POST / PUT / DELETE).

Using ASIHTTPRequest in an iOS project

  • first downlaod it.
  • once you have it downloaded, right click your  project entry in groups and files, select New Group, and name the new group ASIHTTPRequest.
  • drag all of the files from the ASIHTTPRequestClasses directory (except the folders within) into the new ASIHTTPRequest group. Make sure “Copy items into destination group’s folder (if needed)” is selected, and click Finish.
  • repeat this for the two files in ASIHTTPRequestExternalReachability, as these are dependencies of the project.
  • link against CFNetwork, SystemConfiguration, MobileCoreServices and zlib. Choose Project -> Edit Active Target from the menu. Click the plus button, and choose CFNetwork.framework. Then repeat this for SystemConfiguration.framework, MobileCoreServices.framework, and libz.1.2.3.dylib.

Compile your project just to make sure you’re good so far.

Sending a form POST with ASIFormDataRequest

To send POST data in a manner compatible with web page forms, we gonna use the included ASIFormDataRequest subclass. Notice that we are using blocks to setCompletionBlock and setFailedBlock handlers.

- (void) doRequest {

    NSURL *url = [NSURL URLWithString:@"http://localhost/webroot/index.php/test/signUp"];

    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request addPostValue:@"Raul" forKey:@"name"];
    [request addPostValue:@"Hello World" forKey:@"message"];

    [request setCompletionBlock:^{
        NSString *responseString = [request responseString];
        NSLog(@"Response: %@", responseString);
    }];
    [request setFailedBlock:^{
        NSError *error = [request error];
        NSLog(@"Error: %@", error.localizedDescription);
    }];

    [request startAsynchronous];
}

here i’m using a simple CI controller look like this:

input->post('name');
        $message = $this->input->post('message');

        $array = array('result' => true, 'name' => $name."-from controller", 'message' => $message."-from controller");
        echo json_encode($array);
    }
}

/* End of file test.php */
/* Location: ./system/application/controllers/test.php */

when you compile and run your project, you should see some data in your console like this:

2011-03-28 18:03:20.164 ASIHTTPRequestSample[5401:207] Response: {"result":true,"name":"Raul-from controller","message":"Hello World-from controller"}

really nice right? just a couple of lines and you can avoid all of the CFNetwork headche.

download the source here.

Where to get ASIHTTPRequest:

Happy Coding.

Comments